简体   繁体   English

使用jQuery加载外部javascript并禁止“不建议在主线程上使用同步XMLHttpRequest…”

[英]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.

相关问题 不赞成在主线程上使用同步XMLHttpRequest - Synchronous XMLHttpRequest on the main thread is deprecated 不推荐在主线程上使用同步XMLHttpRequest - Synchronous XMLHttpRequest on the main thread is deprecated jQuery load()错误:不推荐使用主线程上的同步XMLHttpRequest - jQuery load() error : Synchronous XMLHttpRequest on the main thread is deprecated WordPress + jQuery Mobile-不赞成在主线程上使用同步XMLHttpRequest吗? - WordPress + jQuery Mobile - Synchronous XMLHttpRequest on the main thread is deprecated? jQuery加载正在触发主线程上的Synchronous XMLHttpRequest不建议警告 - jquery load is triggering Synchronous XMLHttpRequest on the main thread is deprecated warning 主线程上的同步 XMLHttpRequest 在嵌入式 Javascript 代码中已弃用 - Synchronous XMLHttpRequest on the main thread is deprecated within embedded Javascript code 不推荐使用主线程上的Firebase同步XMLHttpRequest - Firebase Synchronous XMLHttpRequest on the main thread is deprecated $.get 不是 function,不推荐使用主线程上的同步 XMLHttpRequest - $.get is not a function, Synchronous XMLHttpRequest on the main thread is deprecated XMLHTTPRequest错误:不建议在主线程上使用同步XMLHttpRequest…(SoundCloud API) - XMLHTTPRequest Error: Synchronous XMLHttpRequest on the main thread is deprecated … (SoundCloud API) 我在加载javascript时遇到问题。 错误:不建议使用[不推荐使用]主线程上的同步XMLHttpRequest - I have a problem whit load to javascript. Error: [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM