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
  • watch the network tab in chrome inspector / firebug Commented Feb 22, 2013 at 5:39
  • How do i know it programatically? Commented Feb 22, 2013 at 5:41
  • You use google stackoverflow.com/questions/3768768/… and close your question Commented Feb 22, 2013 at 5:41
  • That question is related to dymaic loading... Commented Feb 22, 2013 at 5:42

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

Sign up to request clarification or add additional context in comments.

Comments

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 Comments

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
And I think it should be window.clearInterval(checkIfLoaded); (no apostrophes)
-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 Comment

How i do this programatically?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.