The process of loading and rendering HTML by the browser (standard-defined process and optimization of modern browsers), loading and rendering

Source: Internet
Author: User

The process of loading and rendering HTML by the browser (standard-defined process and optimization of modern browsers), loading and rendering

First look at the standard definition of the browser rendering process (found online ):

When the browser opens the webpage, the browser loads and renders the html in sequence. JS loads.
  • Cannot download and parse in parallel (blocking download)
  • The web mode is synchronous. The developer wants to parse and execute the script immediately when parsing a script tag, and blocks the parsing of the document until the script is executed. If the script is externally cited, when JS is referenced, the browser sends a js request and waits until the request is returned. This process is also synchronous, blocking the parsing of the document until the resource is requested. Because the browser needs a stable DOM tree structure, and JavaScript code may directly change the DOM tree structure, such as using document. write or appendChild, or even directly use location. href redirects. In order to prevent JS modifications to the DOM tree, the browser needs to re-build the DOM tree, so it will block other downloads and rendering. This mode has been maintained for many years and is specified in html4 and html5. Developers can mark the script as defer so that it does not block document parsing and will execute the script after the parsing is complete. Html5 adds the option to mark the script as Asynchronous so that the parsing and execution of the script uses another thread.

 

There are several points to note:

1. We know that the processing process of the browser is to parse html to generate a DOM tree-> Generate a render tree based on the DOM tree and style sheet-> render a render tree display. The browser parses html to generate a partial DOM tree so that users can see the page more quickly. Then, the browser generates some render trees and then displays them.

2. There are two types of external resources in this process: Blocking script execution, thus blocking rendering, namely external js and external css. External js blocks DOM tree generation because the browser needs a solid DOM tree, js may damage this structure (of course, it may also change the style [note that it is a style rather than a style table], but this will not affect if it is not blocked ); external css style sheets also block script execution. Theoretically, since style sheets do not change the Dom tree, there is no need to stop document parsing and wait for them. However, there is a problem, the script may request the style information during the parsing process of the document. If the style has not been loaded or parsed, the script will get the wrong value. Obviously, this will cause many problems, this looks like an edge, but it is quite common. Firefox blocks all scripts when a style sheet is loaded and parsed, chrome blocks these scripts only when the script attempts to access certain style attributes that may be affected by unloaded style sheets.

3. other external resources are non-blocking rendering. We can see that the general frame of the page is displayed many times, that is, the position of the image is not displayed, after the image is downloaded, it is re-rendered.

 

Optimization of modern browsers:

Rendering and downloading based on the standard browser. The following code loads external resources in the same order as the resources in html. Where the head adds an external resource request http://hm.baidu.com/hm.js? A041a0f4ff93aef6aa83f34134331a1d should be loaded before all styles

<Html> 

But it is actually in chrome. Firefox, ie8 +, and other browsers found the following results (using https://www.webpagetest.org/test)

  

Why? This isPre-resolution (Speculative parsing)

Webkit and Firefox both perform this optimization. When the script is executed, another thread parses the remaining documents and loads the files to be passedNetworkThe loaded resource. In this way, resources can be loaded in parallel to make the overall speed faster. It should be noted that pre-resolution does not change the Dom tree. It leaves this job to the main parsing process and only parses external resource references, such as external scripts, style sheets, and images.

The figure above shows that a lot of external resource references are resolved during script execution, and the thread is started to download them. The main thread is still waiting for hm. js to return.

Related Article

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.