简体   繁体   English

现代浏览器中的非阻塞javascript和css。还需要吗?

[英]Non-blocking javascript and css in modern browsers. Is it still needed?

I am playing a little with some non-blocking JavaScript loading. 我正在玩一些非阻塞的JavaScript加载。 This means I have a small snippet of JavaScript in my head , and load all my external files at runtime. 这意味着我的JavaScript在我的一个小片段head ,并载入我的所有外部文件在运行时。 I even took it a little further to load CSS non-blocking. 我甚至更进一步加载CSS非阻塞。

I see the articles I could find are a little outdated, that is why I want to know if this is all still relevant. 我看到我能找到的文章有点过时,这就是为什么我想知道这是否仍然相关。

Now first the scripts, they look like this: 现在首先是脚本,它们看起来像这样:

<script>
(function () {
    var styles = JSON.parse(myObject.styles);
    for( name in styles ){
        var link  = document.createElement('link');
        link.setAttribute('rel', 'stylesheet');
        link.setAttribute('type', 'text/css');
        link.setAttribute('href', styles[name]);
        document.getElementsByTagName('head')[0].appendChild(link);
    }

    var scripts = JSON.parse(myObject.scripts);
    for( name in scripts ){
        var e = document.createElement('script');
        e.src = scripts[name];
        e.async = true;
        document.getElementsByTagName('head')[0].appendChild(e);
    }
}());
</script>

myObject.styles is here just an object that holds all the urls for all the files. myObject.styles只是一个包含所有文件的所有url的对象。

I have run 3 test, here are the results: 我已经运行了3次测试,结果如下:

Normal setup 正常设置

页面加载头部有css,底部有javascript

This is just the normal setup, we have 4 css files in the head, and 3 js files at the bottom of the page. 这只是正常的设置,头部有4个css文件,页面底部有3个js文件。

Now I do not see anything blocking. 现在我没有看到任何阻塞。 What I see it that everything is loading at the same time. 我认为一切都在同时加载。

Non-blocking JS 非阻塞JS

使用非阻塞javascript加载页面

Now to take this a little further, I have made ONLY the js files non-blocking. 现在更进一步,我只使js文件非阻塞。 This with the script above. 这与上面的脚本。 I suddenly see that my css files are blocking up the load. 我突然发现我的css文件阻塞了负载。 This is strange, because it is not blocking anything in the first example. 这很奇怪,因为在第一个例子中它没有阻塞任何东西。 Why is css suddenly blocking the load ? 为什么css会突然阻塞负载?

Everything non-blocking 一切都是非阻塞的

页面加载所有内容都是非阻塞的

Finally I did a test where all the external files are loaded in a non-blocking way. 最后,我做了一个测试,其中所有外部文件都以非阻塞方式加载。 Now I do not see any difference with our first method. 现在我认为我们的第一种方法没有任何区别。 They just both look the same. 它们看起来都一样。

Conclusion 结论

My conclusion is that the files are already loaded in a non-blocking way, I do not see a need to add special script. 我的结论是文件已经以非阻塞方式加载,我没有看到需要添加特殊脚本。

Or am I missing something here? 或者我在这里遗漏了什么?

More: 更多:

Yes, in today's browsers, files referenced are being loaded non-blocking way. 是的,在今天的浏览器中,引用的文件是以非阻塞方式加载的。 But there are differences: 但是有区别:

  • ready event appears sooner if you put "things that you do not need to wait for" for dynamic load, as you can see from the timing of the blue bar. 如果你把“你不需要等待的东西”用于动态加载,就会出现就绪事件,正如你可以从蓝条的时间看到的那样。 So actions in the page may start sooner. 因此页面中的操作可能会更快开始。
  • scripts that are loaded from the text in the page (as opposed from dynamic loading) are executed in order. 从页面中的文本加载的脚本(与动态加载相反)按顺序执行。 So they must wait for each other, if someone is loading longer. 因此,如果有人加载时间过长,他们必须等待对方。 Dynamically loaded scripts, on other way, do execute as soon as possible unless put .async=false to script element. 除非将.async=false到脚本元素,否则动态加载的脚本会以其他方式尽快执行。

So, on contemporary browsers, the difference is only semantical (static load simulates old sequential way, dynamic is much more parallel). 因此,在当代浏览器中,差异仅仅是语义上的(静态负载模拟旧的顺序方式,动态更加平行)。

It depends of how many files you want to load in the same time. 这取决于您要在同一时间加载多少个文件。 In your case you are using 3 JavaScript files. 在您的情况下,您使用3个JavaScript文件。 Different browsers have different limits, so it's mean when you have for example 7 JavaScript files in Frefox 7th will be loaded after 6 have finished, since Firefox has limit 6 parallel downloads. 不同的浏览器有不同的限制,所以这意味着当你有7个JavaScript文件在Frefox 7th将在6完成后加载,因为Firefox有限制6个并行下载。

Using scripts or loading scitps just before tag is still good approach. 在标记之前使用脚本或加载scitps仍然是很好的方法。 Try to repeat your test with more JavaScript files, like 10 or so. 尝试使用更多JavaScript文件重复测试,例如10个左右。

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

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