Lt; script defergt; what does defer mean?

Source: Internet
Author: User

Program After the download is complete, parse and execute.
In this document Code If it is not executed immediately, the page will continue to be loaded. When will the code in defer be executed? After all JavaScript code is loaded? After the page is loaded? Before or after onload?

The answer is:

It does not mean that the script is not executed when the webpage is loaded. You can write a script on the webpage and define it, but it is still executed when the webpage is loaded,
Later I found out when I was looking for other things, because it was not a child and I don't know how to use it,
It turns out to be used when you want to dynamically Add a new <SCRIPT> after your webpage has been intercepted. innerhtml = "<script" + "> "... "</script" + "> ";
Then, when you call your new definition, an error will occur, saying that it is still undefined. When you add defer to the above code, you can call it successfully,
In the following code, you can try to remove it, defer> ";, then you will find the problem!
<HTML>
<SCRIPT>
Function insertscript (){
VaR shtml = "<input type = button onclick =" + "go2 ()" + "value = 'click me'> <br> ";
VaR sscript = "<SCRIPT defer> ";
Sscript = sscript + "function go2 () {alert ('Hello from inserted script .')}";
Sscript = sscript + "</script" + "> ";
Scriptdiv. innerhtml = shtml + sscript;
}
</SCRIPT>
<Body onload = "insertscript ();">
<Div id = "scriptdiv"> </div>
</Body>
</Html>

// The defer attribute indicates that the script does not change the document content. Before the document is fully displayed, the JS interpreter does not execute this code.
In this example, because JavaScript does not allow dynamic generation of js code, the code generated after defer is removed is considered invalid. The defer code will be deemed by the interpreter to be skipped during page loading and can be executed.

<SCRIPT src = "../JS/script. js" Defer> </SCRIPT>
The defer function in is to execute the script after the file is loaded, so as to avoid the problem that the object cannot be found.

<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>

In addition, defer is equivalent to executing after the page is completely in, which is equivalent to window. onload, but the application is more flexible than window. onload!

Defer is an "unsung hero" among the powerful functions of the script program ". It tells the browser that the script segment contains code that does not need to be executed immediately and is used together with the src attribute. It can also make these scripts downloaded in the background, the foreground content is displayed to the user normally.
-- The script is executed after the file is loaded.

Note the following two points:
1. Do not call the document. Write command in the defer script section, because document. Write will produce direct output.
2. do not include any global variables or functions to be used to execute the script immediately in the defer script section.

a common method to optimize performance is to set the "Defer" attribute in the

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.