This article is mainly about the role of defer in JavaScript in a detailed analysis of the introduction, the need for friends can come to the reference, I hope to help you.
A lot of people have the use of JavaScript, but see defer may not know what he is used to do; many people have encountered such problems, need to directly execute and manipulate the DOM object JS is always reported to find the object error, The reason we all know is that the page has not finished loading, JS operation object is still in the download. But many people do not know, add defer tag can easily solve this problem. <script src= ". /cgi-bin/delscript.js "defer></script> defer role is that the document is loaded and then executes the script, which avoids the problem of not finding the object---a bit of a problem code as follows: < Button id= "MyButton" onclick= "alert (' OK ')" >test</button> <script> Mybutton.click (); </script> <script> Mybutton.click (); </script> <button id= "MyButton" onclick= "alert (' OK ')" >test</button> <script defer> function Document.body.onload () {alert (document.body.offsetHeight);} </script> plus defer Equal to in the page completely in after the execution, equivalent to Window.onload, but the application is more flexible than window.onload! Defer is a "unsung hero" in the powerful script. It tells the browser that the script section contains code that does not need to be executed immediately, and that it is used in conjunction with the SRC attribute to enable the scripts to be downloaded in the background, and the contents of the foreground are displayed to the user normally. -but the document is loaded and the script is executed Please note two: 1, do not call the document.write command in the defer script segment because document.write will produce a direct output effect. 2, and do not include in the defer script segment any global variables or letters to be used for immediate execution of the scriptNumber. A common way to optimize performance is to set the "defer" property in the <SCRIPT> tab when the script does not need to run immediately. (The script is not included in a function block immediately, so it is executed during the load process.) When you set the Defer property, IE does not have to wait for the script to load and execute. This will make the page load faster. In general, this also means that the immediate script is best placed in a function block and processed in the onload handle of document or body object. It is useful to use this property when there are scripts that need to rely on user action----such as clicking a button, or moving the mouse over a range----. However, when some scripts need to be executed during page loading or after the load is completed, the benefits of using the defer property are not too great. The defer property in script is by default false. According to the description in the DHTML programming book, this is written for the defer attribute: Using the attribute at design time can improve the download performance of a page B Ecause the browser does not need to parse and execute the script and can continue downloading and parsing the page instead . That is, if you are writing a script to add the defer attribute, then the browser will download the script without immediately processing it, but continue to download and parse the page, which can improve the performance of the download. There are many kinds of in this situation. For example, if you define a lot of JavaScript variables, or if you write a lot of scripts in a reference file (. inc) that you need to deal with, you might want to add the defer attribute to these scripts to help improve performance. Examples are as follows: code as follows: <script language= "JavaScript" defer> var object = new Object (); ... </script> because the Defer property defaults to False, here <script language= "JavaScript" defer> Explicit declaration of deferAfter the attribute is equivalent to <script language= "JavaScript" defer=true> declares the defer attribute, you need to determine if there are other variables that refer to the variables in the defer script block. Otherwise, this can result in script errors.