最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

來自國外的頁面JavaScript文件優(yōu)化

 更新時(shí)間:2010年12月08日 16:33:43   作者:  
外部JavaScript文件塊下載和傷害你的頁面的性能,但有一個(gè)簡單的方法來解決此問題:并行使用動(dòng)態(tài)腳本標(biāo)記和加載腳本,提高頁面加載速度和用戶體驗(yàn)。

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:

Timeline - Blocking behavior of JavaScript files

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 <= 7 and Firefox 2, it's two components at a time (following the HTTP 1.1 specs), but both IE8 and FF3 increase the default to 6.

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 advise, you should put the scripts at the bottom of the page, towards the closing </body> tag. This doesn't really make the page load faster (the script still has to load), but helps with the progressive rendering of the page. The user perception is that the page is faster when they can see a visual feedback that there is progress.

Non-blocking scripts

It turns out that there is an easy solution to the download blocking problem: include scripts dynamically via DOM methods. How do you do that? Simply create a new <script> element and append it to the <head>:

var js = document.createElement('script');
js.src = 'myscript.js';
var head = document.getElementsByTagName('head')[0];
head.appendChild(js);

Here's the same test from above, modified to use the script node technique. Note that the third row in the image takes just as long to download, but the other resources on the page are loading simultaneously:

Non-blocking JavaScript timeline

Test example

As you can see the script file no longer blocks the downloads and the browser starts fetching the other components in parallel. And the overall response time is cut in half.

Dependencies

A problem with including scripts dynamically would be satisfying the dependencies. Imagine you're downloading 3 scripts and three.js requires a function from one.js. How do you make sure this works?

Well, the simplest thing is to have only one file, this way not only avoiding the problem, but also improving performance by minimizing the number of HTTP requests (performance rule #1).

If you do need several files though, you can attach a listener to the script's onload event (this will work in Firefox) and the onreadystatechange event (this will work in IE). Here's a blog post that shows you how to do this. To be fully cross-browser compliant, you can do something else instead: just include a variable at the bottom of every script, as to signal “I'm ready”. This variable may very well be an array with elements for every script already included.

Using YUI Get utility

The YUI Get Utility makes it easy for you to use script includes. For example if you want to load 3 files, one.js, two.js and three.js, you can simply do:

var urls = ['one.js', 'two.js', 'three.js'];
YAHOO.util.Get.script(urls);

YUI Get also helps you with satisfying dependencies, by loading the scripts in order and also by letting you pass an onSuccess callback function which is executed when the last script is done loading. Similarly, you can pass an onFailure function to handle cases where scripts fail to load.

var myHandler = {
onSuccess: function(){
alert(':))');
},
onFailure: function(){
alert(':((');
}
};

var urls = ['1.js', '2.js', '3.js'];
YAHOO.util.Get.script(urls, myHandler);

Again, note that YUI Get will request the scripts in sequence, one after the other. This way you don't download all the scripts in parallel, but still, the good part is that the scripts are not blocking the rest of the images and the other components on the page. .

YUI Get can also include stylesheets dynamically through the method YAHOO.util.Get.css() [example].

Which brings us to the next question:

And what about stylesheets?

Stylesheets don't block downloads in IE, but they do in Firefox. Applying the same technique of dynamic inserts solves the problem. You can create dynamic link tags like this:

var h = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.href = 'mycss.css';
link.type = 'text/css';
link.rel = 'stylesheet';
h.appendChild(link);

This will improve the loading time in Firefox significantly, while not affecting the loading time in IE.

Another positive side effect of the dynamic stylesheets (in FF) is that it helps with the progressive rendering. Usually both browsers will wait and show blank screen until the very last piece of stylesheet information is downloaded, and only then they'll start rendering. This behavior saves them the potential work of re-rendering when new stylesheet rules come down the wire. With dynamic <link>s this is not happening in Firefox, it will render without waiting for all the styles and then re-render once they arrive. IE will behave as usual and wait.

But before you go ahead and implement dynamic <link> tags, consider the violation of the rule of separation of concerns: your page formatting (CSS) will be dependent on behavior (JS). In addition, this problem is going to be addressed in future Firefox versions.

Other ways?

There are other ways to achieve the non-blocking scripts behavior, but they all have their drawbacks.

Method Drawback
Using defer attribute of the script tag IE-only, unreliable even there
Using document.write() to write a script tag
  1. Non-blocking behavior is in IE-only
  2. document.write is not a recommended coding practice
XMLHttpRequest to get the source then execute with eval().
  1. eval() is evil”
  2. same-domain policy restriction
XHR request to get the source, create a new script tag and set its content
  1. more complex
  2. same-domain policy
Load script in an iframe
  1. complex
  2. iframe overhead
  3. same-domain policy

Future

Safari and IE8 are already changing the way scripts are getting loaded. Their idea is to download the scripts in parallel, but execute them in the sequence they're found on the page. It's likely that one day this blocking problem will become negligible, because only a few users will be using IE7 or lower and FF3 or lower. Until then, a dynamic script tag is an easy way around the problem.

Summary

  • Scripts block downloads in FF and IE browsers and this makes your pages load slower.
  • An easy solution is to use dynamic <script> tags and prevent blocking.
  • YUI Get Utility makes it easier to do script and style includes and manage dependencies.
  • You can use dynamic <link> tags too, but consider the separation of concerns first.

相關(guān)文章

  • Javascript blur與click沖突解決辦法

    Javascript blur與click沖突解決辦法

    這篇文章主要介紹了Javascript blur與click沖突解決辦法的相關(guān)資料,在開發(fā)過程中經(jīng)常會(huì)遇到blur與click 沖突的情況,這里舉了幾個(gè)例子,和解決辦法,需要的朋友可以參考下
    2017-01-01
  • JavaScript 面向?qū)ο蠡A(chǔ)簡單示例

    JavaScript 面向?qū)ο蠡A(chǔ)簡單示例

    這篇文章主要介紹了JavaScript 面向?qū)ο蠡A(chǔ),結(jié)合簡單實(shí)例形式分析了JavaScript面向?qū)ο蟪绦蛟O(shè)計(jì)中類的定義、類方法與屬性相關(guān)操作技巧,需要的朋友可以參考下
    2019-10-10
  • 老生常談Javascript中的原型和this指針

    老生常談Javascript中的原型和this指針

    下面小編就為大家?guī)硪黄仙U凧avascript中的原型和this指針。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,祝大家游戲愉快哦
    2016-10-10
  • 基于PHP pthreads實(shí)現(xiàn)多線程代碼實(shí)例

    基于PHP pthreads實(shí)現(xiàn)多線程代碼實(shí)例

    這篇文章主要介紹了基于PHP pthreads實(shí)現(xiàn)多線程代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Js參數(shù)值中含有單引號(hào)或雙引號(hào)問題的解決方法

    Js參數(shù)值中含有單引號(hào)或雙引號(hào)問題的解決方法

    本文是對(duì)Js參數(shù)值中含有單引號(hào)或雙引號(hào)問題的解決方法進(jìn)行了總結(jié)介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助
    2013-11-11
  • js根據(jù)鼠標(biāo)移動(dòng)速度背景圖片自動(dòng)旋轉(zhuǎn)的方法

    js根據(jù)鼠標(biāo)移動(dòng)速度背景圖片自動(dòng)旋轉(zhuǎn)的方法

    這篇文章主要介紹了js根據(jù)鼠標(biāo)移動(dòng)速度背景圖片自動(dòng)旋轉(zhuǎn)的方法,實(shí)例分析了javascript操作鼠標(biāo)與圖片的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-02-02
  • JavaScript 事件對(duì)象的實(shí)現(xiàn)

    JavaScript 事件對(duì)象的實(shí)現(xiàn)

    前我寫過一篇關(guān)于JavaScript如何實(shí)現(xiàn)面向?qū)ο缶幊痰奈恼?。今天,我寫這篇文章跟大家討論一下,如何實(shí)現(xiàn)事件。
    2009-07-07
  • 詳解如何使用微信小程序云函數(shù)發(fā)送短信驗(yàn)證碼

    詳解如何使用微信小程序云函數(shù)發(fā)送短信驗(yàn)證碼

    這篇文章主要介紹了詳解如何使用微信小程序云函數(shù)發(fā)送短信驗(yàn)證碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • js使用文檔就緒函數(shù)動(dòng)態(tài)改變頁面內(nèi)容示例【innerHTML、innerText】

    js使用文檔就緒函數(shù)動(dòng)態(tài)改變頁面內(nèi)容示例【innerHTML、innerText】

    這篇文章主要介紹了js使用文檔就緒函數(shù)動(dòng)態(tài)改變頁面內(nèi)容,結(jié)合實(shí)例形式分析了JavaScript使用innerHTML、innerText函數(shù)動(dòng)態(tài)操作頁面元素相關(guān)使用技巧,需要的朋友可以參考下
    2019-11-11
  • 關(guān)于Vue中postcss-pxtorem的使用詳解

    關(guān)于Vue中postcss-pxtorem的使用詳解

    在Web開發(fā)領(lǐng)域,響應(yīng)式設(shè)計(jì)已經(jīng)成為一個(gè)不可或缺的趨勢,PostCSS插件——postcss-pxtorem的出現(xiàn)為我們提供了一種更加智能和高效的解決方案,本文將深入探討postcss-pxtorem的使用,包括其背后的原理、配置選項(xiàng)、實(shí)際應(yīng)用中的注意事項(xiàng)等方面,需要的朋友可以參考下
    2023-12-12

最新評(píng)論

临武县| 财经| 雅江县| 峡江县| 嵩明县| 分宜县| 保山市| 兴安县| 宜宾市| 霍山县| 舒兰市| 洛隆县| 玉屏| 元谋县| 岚皋县| 临西县| 乐都县| 莫力| 淮阳县| 锡林郭勒盟| 彭泽县| 新乐市| 瑞安市| 怀安县| 云龙县| 托克托县| 永善县| 麦盖提县| 临沭县| 尉犁县| 古田县| 江油市| 育儿| 闸北区| 青川县| 平遥县| 石首市| 堆龙德庆县| 于都县| 荣昌县| 彝良县|