对于 IE9 注入脚本标签的执行顺序是否有已知的解决方法?

发布于 2024-11-30 09:18:31 字数 608 浏览 2 评论 0原文

我确信我没有完全理解这个问题,但是似乎我们在我的项目中的 IE9 上看到了奇怪的行为,在某种程度上与通过注入的 JavaScript 的乱序执行有关对 document.write 的调用,例如:

document.write('<scr'+'ipt type="text/javascript" src="'+file1+'"></src'+'ipt>');
document.write('<scr'+'ipt type="text/javascript" src="'+file2+'"></src'+'ipt>');
document.write('<scr'+'ipt type="text/javascript" src="'+file3+'"></src'+'ipt>');

我有限的 Google 研究表明 IE9 将以与其他浏览器(特别是 Firefox 和 Chrome)不同的顺序执行以这种方式注入的脚本。有没有更好的方法来实现我们在这里的目标,这将确保所有浏览器具有相同的执行顺序?

我收回这一点:我们并不真正关心所有浏览器,只关心 Chrome 和 IE9。

I am sure I don't fully understand this problem, but it seems that we are seeing strange behavior on IE9 on my project, somehow related to out-of-order execution of JavaScript that has been injected via calls to document.write, e.g.:

document.write('<scr'+'ipt type="text/javascript" src="'+file1+'"></src'+'ipt>');
document.write('<scr'+'ipt type="text/javascript" src="'+file2+'"></src'+'ipt>');
document.write('<scr'+'ipt type="text/javascript" src="'+file3+'"></src'+'ipt>');

My limited Google research suggests that IE9 will execute scripts injected in this manner in a different order from other browsers (notably, Firefox and Chrome). Is there a better way to achieve what we're going for here, which will ensure the same execution order by all browsers?

I take that back: we don't really care about all browsers, just Chrome and IE9.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

噩梦成真你也成魔 2024-12-07 09:18:31

使用脚本加载器(就像我写的那样:LABjs),它将规范跨各种浏览器加载的所有不同怪癖。还有额外的好处:它不使用那个糟糕的 document.write()。 LABjs 将允许您异步(并行)加载所有脚本,但请确保它们以正确的顺序执行。听起来基本上正是你想要的。

Use a script loader (like the one I wrote: LABjs), which will normalize all the different quirks of loading across the various browsers. And bonus: it doesn't use that god-awful document.write(). LABjs will let you load all your scripts asynchronously (in parallel), but make sure they execute in the proper order. Sounds like basically exactly what you want.

昇り龍 2024-12-07 09:18:31

我想你可以链接一个事件的 onload 事件来启动另一个事件的加载:

var newJS= document.createElement('script');
newJS.onload=function() {alert("done")} //or call next load function
newJS.src="..."
document.body.appendChild(newJS)

I guess you could chain the onload event of one to start the load of another:

var newJS= document.createElement('script');
newJS.onload=function() {alert("done")} //or call next load function
newJS.src="..."
document.body.appendChild(newJS)
妳是的陽光 2024-12-07 09:18:31

因此,以这种方式编写脚本标签的优点是它们 异步加载。我不知道浏览器的细微差别到底是如何完成的,但我认为它们会在下载时执行,没有特定的顺序。与 HTML5 async 属性 的行为类似。

还有另一个 HTML5 属性 defer ,它使脚本按顺序执行,但以非阻塞方式。您可以尝试将其添加到生成的

So the advantage of writing script tags this way is that they are loaded asynchronously. I don't know about browser nuances about exactly how this is done but I would have thought they would be executed when they're downloaded, in no specific order. Similar to the behaviour of the HTML5 async attribute.

There's another HTML5 attribute defer which instead makes scripts execute in order, but in a non blocking way. You could try adding that into your generated <script> tags. IE9 partially honours it.

余生一个溪 2024-12-07 09:18:31

我制作了一个小脚本,正是为了这个目的:

https://github.com/mudroljub/js -async-loader

简而言之,它异步加载所有脚本,然后相应地执行它们。它看起来像这样:

for (var lib in libs) {
    loadAsync(lib);
}

而且你不需要 document.write();

I have made a little script, exactly for this purpose:

https://github.com/mudroljub/js-async-loader

In short, it loads all your scripts asynchronously, and then executes them consequently. It looks something like this:

for (var lib in libs) {
    loadAsync(lib);
}

And you don't need document.write();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文