Defer in Javascript

Source: Internet
Author: User

Usage: <SCRIPT src = "../cgi-bin/delscript. js" Defer> </SCRIPT>
 
Defer is used to execute the script after the file is loaded, so that the object cannot be found.

 

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
These scripts are downloaded in the background, and 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 generate a direct
Output result.
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 <SCRIPT> label when the script does not need to run immediately.
(The script is not included in a function block, so it will be executed during the loading process .)
After setting the "Defer" attribute, ie does not have to wait until the script is loaded and executed. In this way, page loading will be faster. In general, this also indicates that it is best to place the script immediately in the function block and
Onload of the document or body object
Handle. This attribute is useful when some scripts are executed depending on user operations, such as clicking a button or moving the mouse to a certain area. But when some scripts need
The benefits of using the defer attribute are not great during or after page loading.

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.