WebKit原生支持异步载入脚本

 WebKit目前的版本, 已提供支持HTML5的async和defer这两个脚本的属性. ie在很久以前就已经defer属性来即时载入脚本但一直到文档载入后才执行.

<script async src="myAsyncScript.js" onload="myInit()"></script>
<script defer src="myDeferScript.js" onload="myInit()"></script>

Both async and defer scripts begin to download immediately without pausing the parser and both support an optional onload handler to address the common need to perform initialization which depends on the script. The difference between async and defer centers around when the script is executed. Each async script executes at the first opportunity after it is finished downloading and before the window’s load event. This means it’s possible (and likely) that async scripts are not executed in the order in which they occur in the page. The defer scripts, on the other hand, are guaranteed to be executed in the order they occur in the page. That execution starts after parsing is completely finished, but before the document’s DOMContentLoaded event.

 

html5文档中对async和defer这两个属性的描述如下

 

The async and defer attributes are boolean attributes that indicate how the script should be executed. The defer and async attributes must not be specifed if the src attribute is not present.

There are three possible modes that can be selected using these attributes. If the async attribute is present, then the script will be executed asynchronously, as soon as it is available. If the async attribute is not present but the defer attribute is present, then the script is executed when the page has fnished parsing. If neither attribute is present, then the script is fetched and executed immediately, before the user agent continues parsing the page.

 

原文地址: Running scripts in WebKit

XeonWell Studio