Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect whether external script has loaded

Using JavaScript, is there a way to detect whether or not an external script (from a third-party vendor) has completely loaded?

The script in question is used to pull in and embed the markup for a list of jobs and, unfortunately, doesn't make use of any variables or functions. It uses document.write to output all of the content that gets embedded in my page.

Ideally, I'd like to display some kind of loading message while I'm waiting for the external script to load, and if it fails to load, display a "We're sorry, check back later..." message.

I'm using jQuery on the site, but this external script is called before I make the jQuery call.

Here's what the document.write stuff from the external script looks like:

document.write('<div class="jt_job_list">');
document.write("
    <div class=\"jt_job jt_row2\">
        <div class=\"jt_job_position\">
            <a href=\"http://example.com/job.html\">Position Title</a>
        </div>
        <div class=\"jt_job_location\">City, State</div>
        <div class=\"jt_job_company\">Job Company Name</div>
    </div>
");
like image 987
ctrlaltdel Avatar asked Aug 21 '12 15:08

ctrlaltdel


People also ask

How do I check if JavaScript is loaded?

To verify if a JavaScript variable has been loaded or not, we can check if it is undefined or has a null value by doing comparison for both the values. We can also use typeof() since it returns string always to know if variable has been loaded or not.

Do external JavaScript files contain script tags?

Using an external script is easy , just put the name of the script file(our . js file) in the src (source) attribute of <script> tag. External JavaScript file can not contain <script> tags.

Where is the external JavaScript file stored?

The file can be saved anywhere in the Web Project directory. It is common practice to put JavaScript files in a folder named "javascript" or "src" when building web and mobile applications.

What are the 3 different ways an external script can be referenced?

External References An external script can be referenced in 3 different ways: With a full URL (a full web address) With a file path (like /js/) Without any path.


5 Answers

Attach an function to the load event:

<script type="text/javascript" src="whatever.js" onload ="SomeFunction()" />

As far as your loading... problem goes, try displaying a div for loading and then just display:none-ing it in your onload function. Make sure to handle cases where your script fails to load too, though.

like image 91
Phillip Schmidt Avatar answered Oct 19 '22 13:10

Phillip Schmidt


This worked for me: it does however, rely on the newer querySelector interface which most modern browsers support. But if you're using really old browsers, you can use getElement... and run a for loop.

function loadJS(file, callback, error, type) {
    var _file = file ;
    var loaded = document.querySelector('script[src="'+file+'"]') ;

    if (loaded) {
      loaded.onload = callback ;
      loaded.onreadystatechange = callback;
      return
    }

    var script = document.createElement("script");

    script.type = (typeof type ==="string" ? type : "application/javascript") ;

    script.src = file;
    script.async = false ;
    script.defer = false ;
    script.onload = callback ;

    if (error) {
       script.onerror = error ;
       }
    else {
       script.onerror = function(e) {
              console.error("Script File '" + _file + "' not found :-(");
              };
       }

    script.onreadystatechange = callback;

    document.body.appendChild(script);
}
like image 28
Anthony Avatar answered Oct 19 '22 14:10

Anthony


Script tags block downloads, so as long as the content dependent on your script is below where your script it loaded, you should be fine. This is true even if the script is in-line in the body of your page.

This website has a great example of how this works.

This obviously does not work if you're loading the scripts asynchronously.

Scripts without async or defer attributes are fetched and executed immediately, before the browser continues to parse the page.

Source: MDN

like image 43
Nick Avatar answered Oct 19 '22 14:10

Nick


You could put a script block after it on the page:

<script src="external_script.js"></script>
<script type="text/javascript">
    ExternalScriptHasLoaded();
</script>
like image 44
Shmiddty Avatar answered Oct 19 '22 13:10

Shmiddty


Thanks for the assistance above, especially ngmiceli for the Steve Souders link!

I decided to take what's probably a "lazy" approach, and also forego the "loading" message:

$(document).ready(function(){
    if ($('.jt_job_list').length === 0){
        $('#job-board').html("<p>We're sorry, but the Job Board isn't currently available. Please try again in a few minutes.</p>");
    };
});

Pretty simple, but I'm looking to see if an element with the .jt_job_list class is in the dom. If it isn't, I display an error message.

like image 34
ctrlaltdel Avatar answered Oct 19 '22 13:10

ctrlaltdel