检查外部js文件是否已加载

时间:2012-08-21 09:55:53

标签: javascript jquery getscript

ajax请求成功后,我使用

加载脚本
$.getScript(base_url + 'js/js_to_be_loaded.js');

有什么方法可以检查是否已加载该特定脚本?

感谢。

1 个答案:

答案 0 :(得分:2)

您只需要实现success回调函数。

jQuery.getScript( url [, success(script, textStatus, jqXHR)] )

像:

$.getScript(base_url + 'js/js_to_be_loaded.js', onSuccess);

function onSuccess(...)
{
    // in this function you are guaranteed that it is loaded
    // so you can execute code based on the loaded js file or
    // set flags to let your code "know" that it is loaded
}