如何识别脚本中成功加载的javascript文件

如何识别脚本中成功加载的javascript文件,javascript,Javascript,我想从外部服务器添加一个js文件,比如 <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script> 如何识别文件是否通过编程方式成功加载(不在控制台中)?在firefox或chrome中打开页面。并检查网络流量(在firebug或chrome控制台中),查看是否可以看到js文件的内容。在f

我想从外部服务器添加一个js文件,比如

  <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>


如何识别文件是否通过编程方式成功加载(不在控制台中)?

在firefox或chrome中打开页面。并检查网络流量(在firebug或chrome控制台中),查看是否可以看到js文件的内容。

在firefox或chrome中打开页面。并检查网络流量(在firebug或chrome控制台中),查看是否可以看到js文件的内容。

在javascript中,在脚本标记的正下方,可以检查代码中是否可以访问该文件中的某些函数:

if (myFunction){
  //  file loaded successfully
}

其中
myFunction
是该文件中的某个函数

在javascript中,就在您的脚本标记下面,您可以检查该文件中的某个函数是否可以在代码中访问:

if (myFunction){
  //  file loaded successfully
}

如果
myFunction
是该文件中的某个函数,而不使用包管理器(require.js),则它取决于库提供的回调。否则,只需检查函数是否存在,如果不存在,请稍后再试

// 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);

在不使用包管理器(require.js)的情况下,它依赖于库提供的回调。否则,只需检查函数是否存在,如果不存在,请稍后再试

// 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);

观察chrome inspector/Firebug中的网络选项卡我如何通过编程知道它?你使用google并关闭你的问题该问题与dymaic加载相关…观察chrome inspector/Firebug中的网络选项卡我如何通过编程知道它?你使用google并关闭你的问题该问题与dymaic加载相关…我如何做到这一点程序化?我是如何通过程序化实现的?但若文件无法加载,你们的检查将永远持续下去。除非实现了异步加载,否则多次检查是没有意义的,我认为应该是
window.clearInterval(checkiLoaded)(无撇号),但若文件未能加载,则检查将永远持续。除非实现了异步加载,否则多次检查是没有意义的,我认为应该是
window.clearInterval(checkiLoaded)(无撇号)