1

When dynamically adding a script tag to a html page, you can add an on load event to the script tag but this seems to be fired when the script tag has been added to the page but before the contents of the script tag have been passed by the browser.

Is it possible to detect, have an event fired, when the contents of the script tag have been passed by the browser.

I'm thinking of adding a flag to the end of the javascript and just doing a timeout and wait till it exists or times out after a while.

eaglestorm
  • 1,192
  • 1
  • 13
  • 32
  • Check `this.readyState === "loaded" || this.readyState === "complete"` in the onload event, see http://stackoverflow.com/questions/4845762/onload-handler-for-script-tag-in-internet-explorer – MrCode Nov 19 '12 at 10:09
  • thats only for ie in the onload event, in firefox it's undefined – eaglestorm Nov 20 '12 at 09:13

2 Answers2

2

One of the tests that jQuery uses to check if a document is loaded is

if (!document.body) {
    return setTimeout(jQuery.ready, 1);
}

You could in theory do something similar with your scripts by setting a variable to loaded on your JavaScript file

function checkLoaded( ) {

    if ( module && module.loaded ) {
        func(); // function to call after scripts are ready   
    }

    setTimeout( checkLoaded, 1 );

}
Bruno
  • 5,772
  • 1
  • 26
  • 43
  • I think there should be an else there? I think that would keep looping on itself even after the function is defined because there is no short-circuit or else. – Jordon Bedwell Jun 14 '13 at 14:45
0

well if you want to do it only for testing purpose then you can simply try an alert message in the top and bottom of your something.js files , thus whenver your files are loaded it displays the alert messages confirming whether the control goes inside or not. top messages means file is included properly and last message means all the functions in the js files are absolutely fine

it is some layman way and should only be applied only for some ground level testing while developement