js delayed six ways to load

 

S several ways to implement lazy loading, lazy loading js loading speed and will help improve page

JS delay loading, that is, after the page loads, etc. and then load JavaScript files.

JS lazy loading help increase page loading speed.

Generally have the following ways:

defer property

async attribute

Dynamically created DOM way

GetScript method using jQuery

Use setTimeout delay method

Finally, let JS loaded

1. defer property

HTML 4.01 to <script> tag defines the defer attribute. Tag defines defer property element in setting defer attribute tells the browser to download immediately, but delayed execution tag defines defer property.

Uses: show script execution will not affect the structure of the page. In other words, the script execution will be delayed until after the entire page and then parsed completely.

Set defer attribute in <script> element, tells the browser to download immediately, but deferred execution .


 

 

Description: Although the <script> element in the <head> element, but the script contains the browser encounters a delay after the </ html> tag execution.

HTML5 specifications script execution in the order in which they appear. In reality, the script will not necessarily delay the implementation of the order.

defer attribute applies only to external script files . Support will ignore the embedded script defer attribute set to achieve HTML5.

2. async attribute

HTML5 async attribute defines the <script> tag. Similar defer attributes are used to change the behavior of processing the script. Similarly, only applies to external script files . Tag defines async attribute. Similar defer attributes are used to change the behavior of processing the script. Similarly, only applies to external script files .

The purpose: to keep waiting for a page to download and execute the script, which is loaded asynchronously other page content .

Asynchronous script will be executed before the page load event.

We can not guarantee that the script will be executed sequentially.


 

 

async and defer the same, will not block other resources to download, it will not affect the page is loaded.

Disadvantages: can not control the loading of the order

 

 

3. dynamically created DOM way

 

 

4. using jQuery getScript () method

 

 

5. Method of loading time delay setTimeout

Lazy loading js code to the page loads more time

 

 

6. Finally, let JS loaded

The external file js introduced into the bottom of the page, to make the final js introduced to speed up page loading speed

Js e.g. redistributed script file, if placed in the head html, js script is loaded before the page will be loaded into the page, and into the body, it will be run in order to load the page from the fallen JavaScript code

So we can put js files externally introduced into the bottom of the page, to make the final js introduced to speed up page loading speed

3. The above method will allow you to receive occasional Google page speed test tools "lazy loading javascript" warning. So here's the solution will be recommendations from Google Help page.



 

 

This code means waiting until the entire document is loaded exhausted, and then load external files "defer.js".

This code using the steps of:

1). Copy code above

2) Paste the code (HTML document near the bottom of the front HTML tags)

3) Modify the "defer.js" for your external JS file name

4) Make sure you get the file path is correct. For example: If you enter only "defer.js", then "defer.js" certain files and HTML files in the same folder.



note:

This code will load the specified until the document has finished loading external js file. Therefore, should not need to rely on those pages load properly javascript code goes here. The JavaScript code should be divided into two groups. A set of javascript code is needed immediately because the page is loaded, the other group is the javascript code to operate after the page is loaded (for example, add a click event or something else). These need to wait until the page loads JavaScript code before execution, should be placed in an external file, and then imported.

 

在元素中设置defer属性,等于告诉浏览器立即下载,但延迟执行元素中设置defer属性,等于告诉浏览器立即下载,但延迟执行元素中设置defer属性,等于告诉浏览器立即下载,但延迟执行



Guess you like

Origin www.cnblogs.com/yuanjili666/p/12108237.html