The role of defer in JavaScript
<script src= ". /cgi-bin/delscript.js "defer></script>
The defer effect is that the document is loaded and then executed, so that the problem of not finding the object is avoided.
Plus defer equals to be executed after the page is fully in, equivalent to window.onload, but more flexible than window.onload on the application!
Defer is an "unsung hero" in the power of the Scripting program. It tells the browser that the script segment contains code that does not need to be executed immediately, and that it is used in conjunction with the SRC attribute, which also allows the scripts to be downloaded in the background and the contents of the foreground displayed to the user normally.
Please note two points:
1. Do not call the document.write command in the defer script segment, because document.write will produce a direct output effect.
2. Also, do not include any global variables or functions to be used in the defer script segment immediately to execute the script.
One common way to optimize performance is to set the "defer" property in the <SCRIPT> tab when the script does not need to be run immediately. (The script is not included in a function block immediately, so it is executed during the loading process.) 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 indicates that the immediate script is best placed in a function block and processed in the onload handle of the document or Body object. This is useful when there are scripts that need to depend on user action----such as clicking a button, or moving the mouse to an area----use this property. However, when there are scripts that need to be executed during page loading or after loading, the benefits of using the Defer property are not very large.
The defer property in script is false by default. As described in the DHTML Programming Cookbook, this is written for the defer property:
Using the attribute at design time can improve the download performance of a page because the browser does not need to par SE and execute the script and can continue downloading and parsing the page instead.
That is: If you are writing a script to join the Defer property, then the browser will download the script without having to immediately process it, but continue to download and parse the page, which will improve the performance of the download.
There are many kinds of situations like this. For example, if you define a lot of JavaScript variables, or if you write a lot of scripts in the reference file (. inc), you might want to add the defer attribute to these scripts to help with performance improvements.
Examples are as follows:
<script language= "JavaScript" defer>
var object = new Object ();
....
</script>
Because the defer property defaults to False, here
<script language= "JavaScript" defer>
Explicitly declaring an defer property is equivalent to
<script language= "JavaScript" defer=true>
After declaring the defer property, you need to decide if there are any other variables that refer to the variables in the defer script block, otherwise it will cause the script error to occur.
The role of defer in JavaScript