jQuery的.getscript()时,脚本被完全加载和执行回调 [英] jquery .getscript() callback when script is fully loaded and executed

查看:1128
本文介绍了jQuery的.getscript()时,脚本被完全加载和执行回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于从jQuery API页面 http://api.jquery.com/jQuery.getScript/

  

成功回调

     

一旦脚本已经加载,但不一定执行的回调被激发。

  $。getScript加入脚本(test.js,函数(){
    富();
});
 

如果 FOO()函数依赖于test.js,它不能成功执行。

我看到类似的事情在谷歌地图API,但在这种情况下,你可以指定在AJAX脚本URL的回调函数。但总体上有一个明显的方式来等待AJAX​​脚本执行做回调?

解决方案

您可以使用Ajax

  jQuery.ajax({
        异步:假的,
        键入:GET,
        网址:脚本,
        数据:空,
        成功:回调,
        数据类型:脚本
    });
 

异步是假的,因为要加载并执行脚本后,在成功的回调,你可以调用你的函数foo()

As from the jquery api page http://api.jquery.com/jQuery.getScript/

Success Callback

The callback is fired once the script has been loaded but not necessarily executed.

$.getScript("test.js", function() {
    foo();
});

if the foo() function depend on test.js, it cannot be execute successfully.

I saw similar thing with google map api but in that case you can specify the callback function in the ajax script url. But in general is there an obvious way to wait until the ajax script executed to do the callback?

解决方案

You can use ajax

jQuery.ajax({
        async:false,
        type:'GET',
        url:script,
        data:null,
        success:callback,
        dataType:'script'
    });

Async is false, because you want to load and execute the script, after that in success callback you can call your function foo()

这篇关于jQuery的.getscript()时,脚本被完全加载和执行回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆