Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Note: This method <a href="https://code.google.com/p/chromium/issues/detail?id=333318" rel="nofollow">no longer works as of Chrome 36</a>. There are no direct alternatives.</strong></p> <p><sup>Note: The answer below only applies to external scripts, i.e. those loaded with <code>&lt;script src&gt;</code>.</sup></p> <p>In Chrome (and Safari), the "beforeload" event is triggered right before a resource is loaded. This event allows one to block the resource, so that the script is never fetched. In this event, you can determine whether the loaded resource is a script, and check whether you want to perform some action</p> <p>This event can be used to emulate <a href="https://developer.mozilla.org/en-US/docs/Mozilla_event_reference/beforescriptexecute" rel="nofollow">beforescriptexecute</a> / <a href="https://developer.mozilla.org/en-US/docs/Web/Reference/Events/afterscriptexecute" rel="nofollow">afterscriptexecute</a>:</p> <pre class="lang-js prettyprint-override"><code>document.addEventListener('beforeload', function(event) { var target = event.target; if (target.nodeName.toUpperCase() !== 'SCRIPT') return; var dispatchEvent = function(name, bubbles, cancelable) { var evt = new CustomEvent(name, { bubbles: bubbles, cancelable: cancelable }); target.dispatchEvent(evt); if (evt.defaultPrevented) { event.preventDefault(); } }; var onload = function() { cleanup(); dispatchEvent('afterscriptexecute', true, false); }; var cleanup = function() { target.removeEventListener('load', onload, true); target.removeEventListener('error', cleanup, true); } target.addEventListener('error', cleanup, true); target.addEventListener('load', onload, true); dispatchEvent('beforescriptexecute', true, true); }, true); </code></pre> <p>The dispatch times are not 100% identical to the original ones, but it is sufficient for most cases. This is the time line for the (non-emulated) events:</p> <pre class="lang-js prettyprint-override"><code>beforeload Before the network request is started beforescriptexecute Before a script executes afterscriptexecute After a script executes onload After the script has executed </code></pre> <p>Here's an easy way to see that the events are working as expected:</p> <pre class="lang-js prettyprint-override"><code>window.addEventListener('afterscriptexecute', function() { alert(window.x); }); document.head.appendChild(document.createElement('script')).src = 'data:,x=1'; document.head.appendChild(document.createElement('script')).src = 'data:,x=2'; </code></pre> <p>The demo can be seen live at <a href="http://jsfiddle.net/sDaZt/" rel="nofollow">http://jsfiddle.net/sDaZt/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload