gpt4 book ai didi

jquery - 异步加载脚本文件

转载 作者:行者123 更新时间:2023-12-01 04:49:04 24 4
gpt4 key购买 nike

我使用 $.getScript 加载脚本文件,这工作正常。如果我检查浏览器中的网络选项卡,文件确实下载成功。但是,当我尝试使用脚本文件中的函数时,它们是未定义的。 HTML 中也没有新的脚本标记。

新脚本文件下载后如何使用?

注意:下载后,该文件也不会显示在开发工具的资源选项卡中。

编辑:在下面添加了代码和一些更多信息

正在加载的脚本文件:

var STUD = function () {
return {
STUDLoaded: function () {
alert('Working!');
}
}
}

function foo()
{
alert('bar!');
}

js下载脚本文件并调用函数。

$.getScript(gSiteRoot + 'Scripts/Views/' + ControllerName + '.js')
.done(function (script, textStatus) {
debugger;
foo();
if ($('#hdnJSClass') != undefined)
window[$('#hdnJSClass').val()][$('#hdnJSClassFunc').val()]();
})
.fail(function (jqxhr, settings, exception) {
GEN._displayNotification('Error', 'There was an error loading the page, please retry', true);
});

异步加载后调用函数 foo 时会正确触发。所以问题出在窗口调用上,这个调用适用于我的其他 js 函数,但不适用于动态加载的函数。

最佳答案

使用.getScript()所以:

$.getScript('path/to/file.js', function(data, ts, jqxhr){
if( jqxhr.status == 200 ){
// do stuff
}
else{
// failed retrieving file
}
});

关于jquery - 异步加载脚本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23991974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com