Skip to content

Instantly share code, notes, and snippets.

@ishida83
Forked from samthor/safari-nomodule.js
Created February 9, 2020 07:49
Show Gist options
  • Save ishida83/35e2026a9e90cf2f5dc3f5cc19e47857 to your computer and use it in GitHub Desktop.
Save ishida83/35e2026a9e90cf2f5dc3f5cc19e47857 to your computer and use it in GitHub Desktop.

Revisions

  1. @samthor samthor revised this gist May 3, 2017. 2 changed files with 12 additions and 10 deletions.
    11 changes: 6 additions & 5 deletions safari-nomodule.js
    Original file line number Diff line number Diff line change
    @@ -17,16 +17,17 @@
    if (!('noModule' in check) && 'onbeforeload' in check) {
    var support = false;
    document.addEventListener('beforeload', function(e) {
    if (e.target.type === 'module') {
    if (e.target === check) {
    support = true;
    } else if (e.target.hasAttribute('nomodule') && support) {
    e.preventDefault();
    } else if (!e.target.hasAttribute('nomodule') || !support) {
    return;
    }
    e.preventDefault();
    }, true);

    check.type = 'module';
    check.src = URL.createObjectURL(new Blob([], {type: 'text/javascript'}));
    check.src = '.';
    document.head.appendChild(check);
    document.head.removeChild(check);
    check.remove();
    }
    }());
    11 changes: 6 additions & 5 deletions safari-nomodule.min.js
    Original file line number Diff line number Diff line change
    @@ -7,16 +7,17 @@
    if (!('noModule' in c) && 'onbeforeload' in c) {
    var s = false;
    d.addEventListener('beforeload', function(e) {
    if (e.target.type === 'module') {
    if (e.target === c) {
    s = true;
    } else if (e.target.hasAttribute('nomodule') && s) {
    e.preventDefault();
    } else if (!e.target.hasAttribute('nomodule') || !s) {
    return;
    }
    e.preventDefault();
    }, true);

    c.type = 'module';
    c.src = URL.createObjectURL(new Blob([], {type: 'text/javascript'}));
    c.src = '.';
    d.head.appendChild(c);
    d.head.removeChild(c);
    c.remove();
    }
    }());
  2. @samthor samthor revised this gist May 3, 2017. 2 changed files with 23 additions and 14 deletions.
    32 changes: 21 additions & 11 deletions safari-nomodule.js
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,32 @@
    /**
    * Safari 10.1 supports modules, but does not support the `nomodule` attribute - it will
    * load <script nomodule> anyway. This snippet of code corrects tags like-
    * <script type="module" src="..."></script><!-- will load -->
    * <script nomodule src="..."></script> <!-- will not load -->
    * (i.e., both tags must have a src="..." attribute).
    * load <script nomodule> anyway. This snippet solve this problem, but only for script
    * tags that load external code, e.g.: <script nomodule src="nomodule.js"></script>
    *
    * This works because Safari supports the non-standard 'beforeload' event. This allows us
    * to trap the module and nomodule load.
    * Again: this will **not** prevent inline script, e.g.:
    * <script nomodule>alert('no modules');</script>.
    *
    * This workaround is possible because Safari supports the non-standard 'beforeload' event.
    * This allows us to trap the module and nomodule load.
    *
    * Note also that `nomodule` is supported in later versions of Safari - it's just 10.1 that
    * omits this attribute.
    */
    (function() {
    if (!('noModule' in document.currentScript)) {
    var check = document.createElement('script');
    if (!('noModule' in check) && 'onbeforeload' in check) {
    var support = false;
    document.addEventListener('beforeload', function(ev) {
    if (ev.target.type === 'module') {
    document.addEventListener('beforeload', function(e) {
    if (e.target.type === 'module') {
    support = true;
    } else if (ev.target.hasAttribute('nomodule') && support) {
    ev.preventDefault();
    } else if (e.target.hasAttribute('nomodule') && support) {
    e.preventDefault();
    }
    }, true);

    check.type = 'module';
    check.src = URL.createObjectURL(new Blob([], {type: 'text/javascript'}));
    document.head.appendChild(check);
    document.head.removeChild(check);
    }
    }());
    5 changes: 2 additions & 3 deletions safari-nomodule-ext.js → safari-nomodule.min.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,10 @@
    /**
    * Simpler version of above that does not require <script type="module"> *before* a
    * <script nomodule>.
    * Minified-ish version of the above.
    */
    (function() {
    var d = document;
    var c = d.createElement('script');
    if (!('noModule' in c)) {
    if (!('noModule' in c) && 'onbeforeload' in c) {
    var s = false;
    d.addEventListener('beforeload', function(e) {
    if (e.target.type === 'module') {
  3. @samthor samthor revised this gist May 3, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions safari-nomodule-ext.js
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@

    c.type = 'module';
    c.src = URL.createObjectURL(new Blob([], {type: 'text/javascript'}));
    d.head.appendChild(check);
    d.head.removeChild(check);
    d.head.appendChild(c);
    d.head.removeChild(c);
    }
    }());
  4. @samthor samthor revised this gist May 3, 2017. 1 changed file with 23 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions safari-nomodule-ext.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    /**
    * Simpler version of above that does not require <script type="module"> *before* a
    * <script nomodule>.
    */
    (function() {
    var d = document;
    var c = d.createElement('script');
    if (!('noModule' in c)) {
    var s = false;
    d.addEventListener('beforeload', function(e) {
    if (e.target.type === 'module') {
    s = true;
    } else if (e.target.hasAttribute('nomodule') && s) {
    e.preventDefault();
    }
    }, true);

    c.type = 'module';
    c.src = URL.createObjectURL(new Blob([], {type: 'text/javascript'}));
    d.head.appendChild(check);
    d.head.removeChild(check);
    }
    }());
  5. @samthor samthor created this gist May 3, 2017.
    22 changes: 22 additions & 0 deletions safari-nomodule.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    /**
    * Safari 10.1 supports modules, but does not support the `nomodule` attribute - it will
    * load <script nomodule> anyway. This snippet of code corrects tags like-
    * <script type="module" src="..."></script><!-- will load -->
    * <script nomodule src="..."></script> <!-- will not load -->
    * (i.e., both tags must have a src="..." attribute).
    *
    * This works because Safari supports the non-standard 'beforeload' event. This allows us
    * to trap the module and nomodule load.
    */
    (function() {
    if (!('noModule' in document.currentScript)) {
    var support = false;
    document.addEventListener('beforeload', function(ev) {
    if (ev.target.type === 'module') {
    support = true;
    } else if (ev.target.hasAttribute('nomodule') && support) {
    ev.preventDefault();
    }
    }, true);
    }
    }());