JavaScript的文件撰写联脚本执行顺序

JavaScript的文件撰写联脚本执行顺序

问题描述:

我有以下脚本,其中第一和第三 document.writeline 是静态和二是产生

I have the following script, where the first and third document.writeline are static and the second is generated:

<script language="javascript" type="text/javascript">
document.write("<script language='javascript' type='text/javascript' src='before.js'><\/sc" + "ript>");
document.write("<script language='javascript' type='text/javascript'>alert('during');<\/sc" + "ript>");
document.write("<script language='javascript' type='text/javascript' src='after.js'><\/sc" + "ript>");
</script>

Firefox和Chrome将显示的的和的之后的,而Internet Explorer的第一个表演的的只有那么它显示的的和的之后

Firefox and Chrome will display before, during and after, while Internet Explorer first shows during and only then does it show before and after.

我遇到指出,我不是第一次遇到这样的一篇文章,但几乎没有让我感觉更好。

I've come across an article that states that I'm not the first to encounter this, but that hardly makes me feel any better.

有谁知道我可以设置为了确定在所有浏览器,或破解IE像所有其他的,理智的浏览器工作?

注意事项:在code段是一个非常简单的摄制。它是在服务器上生成和所述第二脚本是改变的唯一事情。这是一个漫长的脚本,之前之所以有两个脚本和之后都让浏览器会缓存他们和code的动态部分将尽可能小。它也可能多次出现在不同产生code相同的页面。

Caveats: The code snippet is a very simple repro. It is generated on the server and the second script is the only thing that changes. It's a long script and the reason there are two scripts before and after it are so that the browser will cache them and the dynamic part of the code will be as small as possible. It may also appears many times in the same page with different generated code.

我已经找到了答案更合我胃口:

I've found an answer more to my liking:

<script language="javascript" type="text/javascript">
document.write("<script language='javascript' type='text/javascript' src='before.js'><\/sc" + "ript>");
document.write("<script defer language='javascript' type='text/javascript'>alert('during');<\/sc" + "ript>");
document.write("<script defer language='javascript' type='text/javascript' src='after.js'><\/sc" + "ript>");
</script>

在的和的之后的,直到页面加载完成。

This will defer the loading of both during and after until the page has finished loading.

这将推迟双方的装载

我觉得这是一样好,我可以得到的。希望有人能够给出更好的答案。

I think this is as good as I can get. Hopefully, someone will be able to give a better answer.