JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

13.3.1 Synchronous, Asynchronous, and Deferred Scripts

13.3.1 Synchronous, Asynchronous, and Deferred Scripts


When JavaScript was first added to web browsers, there was no API for traversing and manipulating the structure and content of a document. The only way that JavaScript code could affect the content of a document was to generate that content on the fly while the document was loading. It did this using the document.write() method. Ex ample 13-3 shows what state-of-the-art JavaScript code looked like in 1996.

Example 13-3. Generating document content at load time


Table of Factorials


When a script passes text to document.write(), that text is added to the document input stream, and the HTML parser behaves as if the script element had been replaced by that text. The use of document.write() is no longer considered good style, but it is still possible (see §15.10.2 ) and this fact has an important implication. When the HTML parser encounters a


Both the deferand asyncattributes are ways of telling the browser that the linked script does not use document.write() and won’t be generating document content, and that therefore the browser can continue to parse and render the document while downloading the script. The defer attribute causes the browser to defer execution of the script until after the document has been loaded and parsed and is ready to be manipulated. The async attribute causes the browser to run the script as soon as possible but not to block document parsing while the script is being downloaded. If atag has both attributes, a browser that supports both will honor the async attribute and ignore the defer attribute.

Note that deferred scripts run in the order in which they appear in the document. Async scripts run as they load, which means that they may execute out of order.

At the time of this writing, the async and defer attributes are not yet widely implemented, and they should be considered optimization hints only: your web pages should be designed to work correctly even if deferred and asynchronous scripts are executed synchronously.

13.3 Execution of JavaScript Programs | 319

You can load and execute scripts asynchronously, even in browsers that do not support the asyncattribute, by dynamically creating aelement and inserting it into the document. The loadasync()function shown in Example 13-4 does this. The techniques it uses are explained in Chapter 15 .

Example 13-4. Asynchronously loading and executing a script

// Asynchronously load and execute a script from a specified URL

function loadasync(url) {

var head = document.getElementsByTagName("head")[0]; // Find document

var s = document.createElement("script"); // Create aelement

s.src = url; // Set its src attribute

head.appendChild(s); // Insert theinto head }

Notice that this loadasync() function loads scripts dynamically—scripts that are neither included inline within the web page nor referenced statically from the web page are loaded into the document and become part of the running JavaScript program.

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/27897.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1