1

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!

1
  • You could check if some symbols exported from that library exist. if (window.something3rdpartyprovided) { ... } . Commented May 7, 2015 at 7:06

1 Answer 1

3

Check whether 3rdparty1function has been declared:

if (typeof 3rdparty1function !== "undefined") {
    // library OK
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Your a lifesaver.
A... wait, is (typeof 3rdparty1function === "function") valid also?
@Gideon Yes but the common practice is to test for the definition of the variable. It's best to use it so that other developers recognize your intent.

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.