afterscriptexecute Event

By  on  

Fellow Mozillian Daniel Buchner, curator of the X-Tag project and clever developer who showed us how to detect dom node insertions using CSS animations, recently showed me a new JavaScript feature I'd never heard of:  the SCRIPT element's afterscriptexecute event.  This event, when detected within the document, provides developers insight as to when specific SCRIPT elements are executed.

The following snippet listens for script executions on the page and logs the specific SCRIPT element to the console after executed:

<script id="my_script" type="text/javascript">
document.addEventListener('afterscriptexecute', function(e){
  console.log('Script executed: ', e.target);
}, false);
</script>

<script type="text/javascript">console.log('foo')</script>

<script type="text/javascript">console.log('bar')</script>


This technique will be incredibly useful for debugging complex JavaScript applications, allowing developers to know exactly which script had just been injected and executed.

Daniel has a knack for finding useful new techniques and this tip will be helpful down the road.  Unfortunately only Firefox has implemented this event but I look forward to WebKit support soonish.  Can you think of how you'd use this event?

Recent Features

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • By
    5 HTML5 APIs You Didn&#8217;t Know Existed

    When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It."  Can you blame us though?  We watched the fundamental APIs stagnate for so long that a basic feature...

Incredible Demos

  • By
    Create a Brilliant Sprited, CSS-Powered Firefox Animation

    Mozilla recently formally announced Firefox OS and its partners at Mobile World Congress and I couldn't be more excited.  Firefox OS is going to change the lives of people in developing countries, hopefully making a name for itself in the US as well.  The...

  • By
    Submit Button Enabling

    "Enabling" you ask? Yes. We all know how to disable the submit upon form submission and the reasons for doing so, but what about re-enabling the submit button after an allotted amount of time. After all, what if the user presses the "stop"...

Discussion

  1. It would appear from reading the spec (step 9 of http://www.w3.org/html/wg/drafts/html/master/scripting-1.html#the-script-element) that the only difference between this event and the load event (aka “onload”) is that load only fires for externally loaded script files, whereas this event fires for both external and inline script elements.

    So, one could conclude that this event is only useful if you care to track the execution of inline script elements, otherwise you’d just use the much more ubiquitous and well known load event. I’m not really able to think of a great use case for tracking inline script elements finishing executing. Almost all such tracking of an inline script element would be from a script loader or dependency manager or other such code generator which created a new inline script element and did script injection to execute it. But in those cases, since JS is single threaded, and since such inline injection executions happen synchronously, you already “know” when that inline snippet finishes running because that’s the next line of your code after you injected it. :)

    I suppose some niche uses might involve passively observing all script element behavior on the page, or developer tools, or things like that. But I think from what I understand, this event probably won’t have much added value for general developers over load, and could quite possibly confuse things if someone listened to both events.

    • Brandon

      Just wanted to add some details to what Kyle said, the fact that this event allows users to know when inline scripts ran is already a use that no other method could provide, especially important for software development (add-ons).

      Furthermore, unlike the load event, afterscriptexecute will only trigger when scripts executes, while the load event will also trigger for every single image that loads, or other element that triggers the same event. As such, afterscriptexecute is focused just on what the user wants and is less resource intensive than load on a page that has more than script elements triggering the same events.

      This event, along with beforescriptexecute, are not just for some niche uses, they are in fact very useful for developers and its value is everything but irrelevant, especially since there isn’t any other way of listening to inline scripts unless the developer wrote it himself.

  2. beforescriptexecute works too! So you can get when the script loaded and how long it took to execute. Pretty cool.

    var time = new Date().getTime();
    var start;
    document.addEventListener('beforescriptexecute', function(e){
    	start = (new Date().getTime() - time);
    }, false);
    document.addEventListener('afterscriptexecute', function(e){
    	var end = new Date().getTime() - time;
    	var total = end - start;
    	console.log('Script executed: ', e.target.src, start, end, '( '+total+' )');
    }, false);
    
  3. joe

    Hi Mike,
    beforescriptexecute ??
    Do not work for me.
    (Firefox 17.0.1 and win7)

  4. qgustavor

    There are security issues related to this?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!