[英]Loading external javascript with jQuery and suppress “Synchronous XMLHttpRequest on the main thread is deprecated…”
I've been reading several posts on this topic but haven't been able to find a solution yet. 我已经阅读了有关该主题的几篇文章,但尚未找到解决方案。 I'm using jQuery to set up a simple plugin which depends on some other files as well (for example a language file).
我正在使用jQuery设置一个简单的插件,该插件也依赖于其他文件(例如语言文件)。 I'd like to provide an as easy as possible set up for the end user so I would like to dynamically load those javascript files.
我想为最终用户提供一个尽可能简单的设置,所以我想动态加载那些javascript文件。 When I do this I either run into the warning
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.
当我这样做时,我要么遇到警告
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.
or I have to delay my script because the asynchronous call isn't yet completed at the point where I need the contents of the file included. 否则我必须延迟脚本,因为在需要包含文件内容的时候异步调用尚未完成。
I've tried several ways to achieve this without delaying the script: 我尝试了几种方法来实现此目标而不延迟脚本:
Attempt 1. By appending to the body 尝试1.通过附加到身体
$('body', document).append('<script type="text/javascript" src="myfile.js"></script>');
Attempt 2 - By using $.getScript 尝试2-通过使用$ .getScript
$.ajaxSetup({ async: false });
$.getScript('myfile.js')
.done(function(script, textStatus) {
// success code... map variables
})
.fail(function(jqxhr, settings, exception) {
if( window.console ) {
console.log('error loading the file);
}
});
Attempt 3 - By using $.ajax, roughly the same as $.getScript 尝试3-通过使用$ .ajax,与$ .getScript大致相同
$.ajax({
url: 'myfile.js',
dataType: 'script',
async: false,
success: function(script, textStatus) {
// success code... map variables
}
});
Attempt 4 - using $.holdReady() but this seems ineffective since the plugin is only initiated within $(document).ready(function() { $('.myDemo').myplugin(); }
. 尝试4-使用$ .holdReady(),但这似乎无效,因为插件仅在
$(document).ready(function() { $('.myDemo').myplugin(); }
。
$.holdReady( true );
$.getScript( 'myfile.js', function() {
// success code.. map variables
$.holdReady( false );
});
How should an external file be attached/included without having to delay the script and not to ensure the warning isn't fired. 如何在不延迟脚本的情况下附加/包括外部文件,并且不确保未发出警告。 Is this even possible or am I asking for water to be frozen and melted at the same time?
这是否有可能或者我是否要求同时冻结和融化水?
I've now fixed it nesting ajax requests and building upon the complete
-callback. 我现在修复了嵌套ajax请求并基于
complete
-callback的问题。 I'm not sure if this is the most desireable way but with two external files (for now) it's maintainable. 我不确定这是否是最可取的方法,但是使用两个外部文件(目前)可以维护。
There's a downside; 有一个缺点。 at this moment for each instance of the plugin two files are requested.
目前,对于插件的每个实例,需要两个文件。 So some sort of cache-variable should also be implemented and based on if the files are loaded already they should or shouldn't be loaded the next iteration.
因此,还应该实现某种缓存变量,并根据文件是否已加载来确定是否应该在下一次迭代中加载它们。
Other suggestions are welcome! 欢迎其他建议!
$.ajax({
url: 'firstfile.js',
dataType: 'script',
success: function() {
// success code.. map variables
},
complete: function() {
$.ajax({
url: 'secondfile.js',
dataType: 'script',
success: function() {
// success code.. map variables
},
complete: function() {
// call function to continue the procedure
}
})
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.