eaglestorm
eaglestorm

Reputation: 1202

Is it possible to detect when the contents of a script tag have been loaded and then passed by the browser

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.

Upvotes: 1

Views: 422

Answers (2)

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

Upvotes: 0

Bruno
Bruno

Reputation: 5822

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 );

}

Upvotes: 2

Related Questions