来自国外的页面JavaScript文件优化_javascript技巧
The problem: scripts block downloads
Let's first take a look at what the problem is with the script downloads. The thing is that before fully downloading and parsing a script, the browser can't tell what's in it. It may contain document.write()
calls which modify the DOM tree or it may even contain location.href
and send the user to a whole new page. If that happens, any components downloaded from the previous page may never be needed. In order to avoid potentially useless downloads, browsers first download, parse and execute each script before moving on with the queue of other components waiting to be downloaded. As a result, any script on your page blocks the download process and that has a negative impact on your page loading speed.
Here's how the timeline looks like when downloading a slow JavaScript file (exaggerated to take 1 second). The script download (the third row in the image) blocks the two-by-two parallel downloads of the images that follow after the script:
Here's the example to test yourself.
Problem 2: number of downloads per hostname
Another thing to note in the timeline above is how the images following the script are downloaded two-by-two. This is because of the restriction of how many components can be downloaded in parallel. In IE
You can work around this limitation by using multiple domains to host your components, because the restriction is two components per hostname. For more information of this topic check the article “Maximizing Parallel Downloads in the Carpool Lane” by Tenni Theurer.
The important thing to note is that JavaScripts block downloads across all hostnames. In fact, in the example timeline above, the script is hosted on a different domain than the images, but it still blocks them.
Scripts at the bottom to improve user experience
As Yahoo!'s Performance rules advise, you should put the scripts at the bottom of the page, towards the closing
上一篇: MySQL增量备份
下一篇: php-fpm的安装和启动
推荐阅读
-
javascript - PHP或者JS如何跳转来自chrome Opera 等手机模拟浏览器的用户到不同的页面
-
IE6中使用position导致页面变形的解决方案(js代码)_javascript技巧
-
js计算页面刷新的次数_javascript技巧
-
js或css文件后面跟参数的原因说明_javascript技巧
-
避免回车键导致的页面无意义刷新的解决方法_javascript技巧
-
js检查页面上有无重复id的实现代码_javascript技巧
-
全面理解面向对象的 JavaScript(来自ibm)_javascript技巧
-
js获取判断上传文件后缀名的示例代码_javascript技巧
-
js的压缩及jquery压缩探讨(提高页面加载性能/保护劳动成果)_javascript技巧
-
网站繁简切换的JS遇到页面卡死的解决方法_javascript技巧