Script Async Properties Load scripts in non-blocking mode _javascript tips

Source: Internet
Author: User

1.HTML5 implements the script's Async property , this new attribute allows JS to load in a non-blocking mode in the browser, and the script has a defer attribute, This property is currently implemented by all browsers (except for Firefox and earlier versions of Chrome), IE is doing well and supports attributes from the outset.

Copy Code code as follows:

Async
<script Async src= "Dquery.js" async></script>

Defer
<script Async src= "Dquery.js" defer>
</script>

the difference between 2.async and defer
Script with async or defer immediately downloads without blocking page parsing, and provides an optional onload event handler that is invoked after the script download completes to perform some initialization work related to this script. The difference is that async executes immediately (before the Window.onload event) as soon as the download is complete, and defer ensures that JS is executed in the order in which it was performed in the page (before the Domcontentloaded event).
3. To resolve browser compatibility issues, you can use the following code to process
Copy Code code as follows:

function Lazyload () {
var elem = document.createelement ("script");
Elem.type = "Text/javascript";
Elem.async = true;
ELEM.SRC = "js/dquery.js?v=11"; The corresponding JS file
Document.body.appendChild (Elem);
}

if (Window.addeventlistener) {
Window.addeventlistener ("Load", Lazyload, false);
else if (window.attachevent) {
Window.attachevent ("onload", lazyload);
} else {
Window.onload = Lazyload;}

Because it is asynchronous loading, the use of the corresponding JS content needs to be used in the Window.onload event, written directly in the page will report script errors, such as:

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.