简体   繁体   English

对非阻塞脚本的痴迷

[英]Obsession with non-blocking scripts

Since i discovered the concept of non-blocking scripts i have become obsessed with loading all my external scripts this way. 由于我发现了非阻塞脚本的概念,我已经开始着迷于以这种方式加载所有外部脚本。 I have even hacked Joomla! 我甚至砍了Joomla! templates(which i know is a bad practice) in order to load non-blocking scripts in the index.php file. 模板(我知道这是一个不好的做法),以便在index.php文件中加载非阻塞脚本。 Example code below. 示例代码如下。

(function() {
    var script = document.createElement('script'),  head = document.getElementsByTagName('head')[0]; 

    script.type = 'text/javascript'; 
    script.src = "http://www.mywebsite.com/scripts/one_of_many.js"
    head.appendChild(script);
})();

My questions are: 我的问题是:

When is it good/bad to load non-blocking scripts? 何时加载非阻塞脚本是好还是坏?

What should be the limit for using non-blocking scripts? 使用非阻塞脚本的限制应该是什么?

The technique you're using for non-blocking scripts (appending a script DOM element) will not keep script execution order on all browsers, only on Firefox and Opera. 您用于非阻塞脚本(附加脚本DOM元素)的技术不会在所有浏览器上保留脚本执行顺序,仅限于Firefox和Opera。

If you don't care about execution order then you can use it safely. 如果您不关心执行顺序,那么您可以安全地使用它。

If not, you can combine it with some other techniques like script defer for IE, script in iframe or XHR. 如果没有,您可以将其与其他一些技术结合使用,例如IE的脚本延迟,iframe中的脚本或XHR。

More details on Even Faster Websites 关于更快网站的更多细节

If you are loading lots of files even as non-blocking, which you can achieve by just by putting them at the end you html just before the </body> tag since browsers do things as they find them on the page, is if you are loading quite a few. 如果您正在加载大量文件,即使是非阻塞文件,只需将它们放在</body>标记之前的html即可实现,因为浏览器会在页面上找到它们时执行操作,如果您装载了不少。

Its good to do that but its better to see about merging files and minifying/obstuficating to get even more time shaved off. 它很好,但它更好地看到合并文件和缩小/障碍,以减少更多的时间。 The other obvious thing is to make sure that you are GZipping the JS that goes to the browser. 另一个显而易见的事情是确保你正在GZipping JS到浏览器。

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

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