Gideon
Gideon

Reputation: 1507

Determine if external javascripts are loaded correctly

I have some external thirdparty js files on my page, let say:

3rdparty1.js

And also I have one of my own:

myown1.js

I want to perform a condition in myown.js that asks if 3rdparty1.js is successfully loaded or not (e.g. No internet connection, error 404, suddenly third party makes 3rdparty1 private, etc.) similar to this code:

// inside myown1.js
if(3rdparty1.js_is_loaded_correctly) {

    // performs function found inside 3rdparty1.js
    3rdparty1function();
} else {

    // performs an alternative function (maybe found inside myown.js)
    myownjsbackupfunction();

    // or maybe show prompt messages
    alert("Some functionalities are not available at the moment."
        + " Try reloading this page later.");
}

Now, how do you write the proper condition statement for this? Note that 3rdparty1.js is on a separate domain, if that helps.

Thanks!

Upvotes: 1

Views: 45

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382102

Check whether 3rdparty1function has been declared:

if (typeof 3rdparty1function !== "undefined") {
    // library OK

Upvotes: 3

Related Questions