An example of what the JS script defer does

  • 2020-03-30 01:10:05
  • OfStack

 
<script src="../cgi-bin/delscript.js" defer></script> 
 In the defer The effect is to load the document and then execute the script , This will avoid the problem of missing the object --- A little problem  
<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 is equal to waiting until the page is fully loaded, which is equivalent to window.onload, but more flexible than window.onload!

Defer is an unsung hero of scripting's power. It tells the browser that the Script segment contains code that does not need to be executed immediately, and, when used in conjunction with the SRC attribute, it also enables the scripts to be downloaded in the background while the foreground content is displayed to the user.
-- but don't execute the script until the document is loaded

Two final points:

1. Do not call the document.write command in the script section of defer type, because document.write produces direct output.
2. Also, do not include any global variables or functions in the defer script section to use for immediate script execution.

A common way to optimize performance is when a script does not need to be run immediately. SCRIPT> Set the "defer" property in the tag. The immediate script is not included in a function block and is therefore executed during the load. After setting 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 the function block and handles the function in the onload handle of the document or body object. This is useful when there are scripts that rely on user actions to execute -- such as clicking a button, or moving the mouse over an area. But when there are scripts that need to be executed during or after the page loads, the benefit of using the defer property is not so great.

It's all the trouble with defer

< P style = "line - height: 150%; The text text-indent: 15 pt; '> Over the weekend, Google ads were placed on this site. In order to prevent the investment of ads from prolonging the page import time, the Google Ad code was modified. Script> Use the defer delay import advertising, I have been using the FireFox browser, hasn't been found that there is a problem, today, the friend check information on this site, she USES the Maxthon, let me see the article, the page is a flash in the past, this just know there is a problem, I should say that this has been in such a site visit two days off, check the reason, only to find that is why the defer, because in Firfox defer as if failure, page is actually loaded with original content, and in IE, due to delay load the script, for Google For the Ad script, it USES the document.write method, and because of defer, the script for Google Ad is detected as an iframe, so when you get the actual content of the Google Ad, you cover the entire page, resulting in a flash view of the full text. < / p>

< P style = "line - height: 150%; The text text-indent: 15 pt; '> This is because I modified the code not so test, no matter what to change in the future need to be tested in IE and FireFox to pass, for these two days caused everyone access difficulties I would like to express my sincere apology! < / p>
< P style = "line - height: 150%; The text text-indent: 15 pt; '> In addition: we recommend you to use FireFox to visit the site, access and resolution speed and page layout consistency are better! < / p>

Related articles: