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

Stack User 5674
  • 1,548
  • 1
  • 20
  • 44

3 Answers3

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

Ramunas
  • 3,853
  • 1
  • 30
  • 31
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);
andyzinsser
  • 2,463
  • 2
  • 18
  • 18
  • 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 Feb 22 '13 at 06:17
  • And I think it should be `window.clearInterval(checkIfLoaded);` (no apostrophes) – Ramunas Feb 22 '13 at 06: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.

spiritwalker
  • 2,257
  • 14
  • 9