简体   繁体   English

IE间歇性地不执行动态添加的脚本文件

[英]IE intermittently doesn't execute a dynamically added script file

We have some JavaScript that writes a script include to a dynamic resource in our web page in order to allow us to communicate some information between pages served from different servers that are subject to cross site scripting restrictions. 我们有一些JavaScript将脚本包括在我们的网页中的动态资源中,以便允许我们在不同服务器提供的页面之间传递一些信息,这些服务器受到跨站点脚本限制。

The idea is that the browser requests the JavaScript file which is served by a dynamic resource on the server side (which also puts some server side information into the Request). 这个想法是浏览器请求由服务器端的动态资源提供服务的JavaScript文件(它还将一些服务器端信息放入请求中)。 The JavaScript file is then executed by the browser when it is added to the page. 然后,当浏览器将JavaScript文件添加到页面时,将执行该JavaScript文件。

We've run into an issue with Internet Explorer where the JavaScript returned in the response is intermittently not executed when it is added to the page. 我们遇到了Internet Explorer的问题,当响应中返回的JavaScript在添加到页面时间歇性地不执行时。 Inspecting a Fiddler HTTP trace when the problem occurs shows the script is successfully returned to the browser. 发生问题时检查Fiddler HTTP跟踪显示脚本已成功返回到浏览器。

To test this more reliably, I altered the code that adds the script to run 1000 times in a loop as below: 为了更可靠地测试这个,我改变了添加脚本的代码,在循环中运行1000次,如下所示:

for (var i = 1; i <= 1000; i++) {
    try {
        var script = document.createElement("SCRIPT");
        script.src = serverHome + "/ajavascriptfile.js?token=" + token + "&num=" + i;
        script.id = token;
        document.getElementsByTagName("HEAD")[0].appendChild( script );
    } catch (e) {
        alert(e);
    }
}

The script returned by ajavascriptfile.js simply increments a counter on my page: ajavascriptfile.js返回的脚本只是递增我页面上的计数器:

var output = document.getElementById("output");
output.innerHTML = parseInt(output.innerHTML) + 1;

No exceptions are ever caught or alerted in this test. 在此测试中没有捕获或警告异常。

If this executes properly the counter should get to 1000 (which it does in Firefox). 如果这正确执行,计数器应该达到1000(它在Firefox中执行)。 However in IE6 it averages 900-950, IE7 is around 995-998 and IE8 is a shocking 750-800. 然而在IE6中平均为900-950,IE7为995-998,IE8为750-800。

Has anyone else encountered Internet Explorer not executing dynamically included scripts? 有没有其他人遇到Internet Explorer没有执行动态包含的脚本? If so, do you know how to workaround this problem? 如果是这样,你知道如何解决这个问题吗?

It may be that the scripts are not queueing in the order they are added to the head, but are being interpreted as soon as they're activestate is complete, either from the cache or a download. 可能是脚本没有按照它们添加到头部的顺序排队,但是一旦它们的活动状态完成,就会从缓存或下载中解释。 If the script you asked for last loads first, it can cause a problem. 如果您要求的最后一个脚本首先加载,则可能会导致问题。

IE8 allows 6 concurrent scripts, I think IE7 allowed 4, and 6 allowed 2. IE8允许6个并发脚本,我认为IE7允许4个,6个允许2个。

I have seen this in Opera, Chrome and Safari as well, (but not firefox yet) so if I am loading more than one script, I hold running any commands until I know the resources are available- usually by testing the typeof a function from the required file, and a timer callback if not found. 我在Opera,Chrome和Safari中也见过这个,(但还没有firefox),所以如果我加载多个脚本,我会保持运行任何命令,直到我知道资源可用 - 通常是通过测试函数的类型所需文件,如果找不到,则返回计时器回调。

Internet Explorer may be caching the script file. Internet Explorer可能正在缓存脚本文件。 Try adding some additional entropy to the src include: 尝试在src中添加一些额外的熵包括:

script.src = serverHome + "/ajavascriptfile.js?token=" + token + "&num=" + i + '&r=' + Math.random();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM