简体   繁体   English

如果使用document.write,javascripts是否同步加载?

[英]Are the javascripts loaded synchronously if one uses document.write?

Is it the same if you load javascript to the page like this: 如果你像这样加载javascript到页面是否相同:

<script src="http://domain.net/script.js" type="text/javascript"></script>

and like this 并且喜欢这个

<script type="text/javascript">
  document.write("<script src='http://domain.net/script.js' type='text/javascript'></script>");

I have a lot of the dependencies in js and want to configure then in as a separate array, which are just bundled in index.html in the way like: 我在js中有很多依赖项,并希望将其配置为一个单独的数组,它们只是在index.html中捆绑,如:

<script type="text/javascript">
  for (i in config.files.javascripts) {
    document.write("<script src='config.files.javascripts[i]' type='text/javascript'></script>");
  }
</script>

The question is - should it work as expected so that the next file is not executed until the previous one is loaded? 问题是 - 它是否应按预期工作,以便下一个文件在上一个文件加载之前不会被执行?

This is there is no server side in the front-end I'm doing and it will also sometimes work as widget though for the dev and test we should load files unmerged and uncompressed to test. 这是我正在做的前端没有服务器端,它有时也可以作为小部件工作,但对于开发和测试我们应该加载未合并和未压缩的文件进行测试。

Thanks! 谢谢!

To answer your question: Yes, scripts loaded with document.write are guaranteed to execute in the order written. 回答你的问题:是的,用document.write加载的脚本保证按写入的顺序执行。

Same applies to scripts loaded using the W3C DOM methods. 同样适用于使用W3C DOM方法加载的脚本。

var scriptElm = document.createElement('script');
scriptElm.defer = true; // Causes faster (parallel) loading in some browsers.
scriptElm.src = 'path/to/script.js';
document.getElementsByTagName('head')[0].appendChild( scriptElm );

That said, document.write() is generally considered really bad practice (for a variety of reasons) and can lead to breakage and annoying edge-case problems later on. 也就是说, document.write()通常被认为是非常糟糕的做法(出于各种原因),并且可能导致以后出现破损和烦恼的边缘案例问题。 I, therefore, recommend that you use the W3C DOM injection shown above, if nothing else. 因此,我建议您使用上面显示的W3C DOM注入,如果没有别的话。

- - - - - - - -
Then there are script loading libraries that make this sort of resource loading easier, more elegant, and sometimes even faster. 然后是脚本加载库,使这种资源加载更容易,更优雅,有时甚至更快。 Some load scripts in parallel but execute in a predictable order - like the one already linked to by @CoBaLt2760 http://www.dustindiaz.com/scriptjs 有些并行加载脚本但是以可预测的顺序执行 - 就像已经链接到@ CoBaLt2760的那样http://www.dustindiaz.com/scriptjs

jQuery also has a very simple method $.getScript( 'path/to/script.js' ); jQuery还有一个非常简单的方法$.getScript( 'path/to/script.js' ); ( documentation ) that you can use. 文档 )您可以使用。 If memory serves me right, It uses simple W3C DOM injection. 如果内存对我有用,它使用简单的W3C DOM注入。

Google "javascript loading" or some such, or search here on StackOverflow,com if you're interested in more links to such libraries. 谷歌“javascript加载”或其他一些,或在StackOverflow上搜索,如果您对这些库的更多链接感兴趣,请参阅。

If you are looking to load asynchronously javascript scripts, in the right order, I shall recommend you $script.js developed by the Twitter javascript lead dev http://www.dustindiaz.com/scriptjs 如果你想以正确的顺序加载异步javascript脚本,我会推荐你​​使用Twitter javascript开发者开发的$ script.js http://www.dustindiaz.com/scriptjs

It's really great!! 真的很棒!

if your config.files.javascripts is an Array, then you shouldn't use for ... in , as that syntax is intended for looping over named keys in objects. 如果您config.files.javascripts是一个数组,那么你不应该使用for ... in ,因为这句法是用于在物体上循环命名的键。

For Arrays use a traditional C style for loop syntax: 对于Arrays,使用传统的C风格循环语法:

for (var i=0; i<array.length; i++) {

So for your particular case, this should do the trick: 所以对于你的特殊情况,这应该做的伎俩:

var headerElm = document.getElementsByTagName('head')[0],
    scripts = config.files.javascripts;

for (var i=0, l=scripts.length; i<l; i++)
{
  var scriptElm = document.createElement('script');
  scriptElm.defer = true; // causes faster (parallel) loading in some browsers.
  scriptElm.src = scripts[i];
  headerElm.appendChild( scriptElm );
}

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

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