确定是否正确加载了外部javascripts

时间:2015-05-07 07:04:51

标签: javascript

我的页面上有一些外部第三方js文件,请说:

3rdparty1.js

我也有自己的一个:

myown1.js

我想在myown.js中执行条件,询问3rdparty1.js是否成功加载(例如,没有互联网连接,error 404,突然第三方将3rdparty1设为私有等)与此代码类似:

// 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.");
}

现在,您如何为此撰写适当的条件声明?请注意,3rdparty1.js位于单独的域中,如果有帮助的话。

谢谢!

1 个答案:

答案 0 :(得分:3)

检查是否已声明3rdparty1function

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