0

I want to add a js file from external server like

  <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>

How i can Identify the file is succesfully loaded programatically(Not in the console)?

4

3 Answers 3

3

In javascript right under your script tag you could do a check if some function in that file is accessible in your code:

if (myFunction){
  //  file loaded successfully
}

where myFunction is some function in that file

2

Without using a package manager (require.js), it depends on the callbacks provided by the library. Otherwise, just check to see if a function exists, if not, try again in a second

// Stop checking after 10 seconds
var scriptTimedOut = window.setTimeout(function() {
    window.clearInterval(checkIfLoaded);
    alert("Script took more than 10secs, so give up");
}, 10000);

// Check every 1 second if the function exists in the script yet
var checkIfLoaded = window.setInterval(function() {
    if (someFunctionInTheScript) {
        // The script is loaded so stop checking
        window.clearInterval(checkIfLoaded);
        window.clearTimeout(scriptTimedOut);
    }
}, 1000);
2
  • But if file fails to load your checking will last forever. And there is not point in checking more that once unless async loading is implemented
    – Ramunas
    Commented Feb 22, 2013 at 6:17
  • And I think it should be window.clearInterval(checkIfLoaded); (no apostrophes)
    – Ramunas
    Commented Feb 22, 2013 at 6:19
-1

open your page in firefox or chrome. and check net traffic(in firebug or chrome console) and see if you can see the content of the js file.

1
  • How i do this programatically? Commented Feb 22, 2013 at 5:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.