Javascript execution in the browser (text)

Source: Internet
Author: User

1. Components of most browsers


The three components at the bottom layer are the network, UI backend, and js interpreter. The function is as follows:
(1) network-used to complete network calls, such as http requests. It has platform-independent interfaces and can work on different platforms.
(2) UI backend: it is used to draw basic components such as a combination selection box and a dialog box. It has a common interface not specific to a platform and uses the user interface of the operating system at the underlying layer.
(3) JS interpreter-used to explain and execute JS Code

Ps: And THE knowledge points mainly come from "how browsers work: behind the scenes of modern web browsers". If you want to learn more, please take a look.
2. Most browsers (such as chrome) allow a single thread to execute javascrip and update the user interface. This thread is often called a "UI thread of the browser" and can only execute one of the operations at a time. This means that when Javascript code is being executed, the user interface cannot respond to the input, and vice versa. This is because the role of javascript code is to operate DOM to update the user interface. Using the same thread to take charge of these two tasks can be more efficient.
3. The UI thread of the browser is based on a simple queue system. tasks are saved to the queue until the process is idle. Once idle, the next task in the queue is extracted and run again. These tasks either run javascript code or execute UI updates, including re-painting and re-arrangement.
4. Emphasize that javascript is run in a single thread. Do not be confused by functions such as setTimeout () and setInterVal () and mistakenly think it is a multi-thread.
OK. The basic point is explained. Let's go to the topic to explain the javascript Execution Process in the browser.
I. Principles
Generally, each time a tag appears, the page waits for parsing and execution of the script, regardless of whether the current Javascript is embedded or contains an external link file, the download and rendering of the page must both stop and wait for the script to be executed. This is necessary in the page lifecycle, because the page content may be modified during script execution. A typical example is to use document. write () on the page ().
This is easy to understand when javascript code is embedded in html, but it is a little load when javascript is an external link file, because there is a loading process, besides, the browser Often caches the js file after loading it.
First, we use the following example to illustrate the cache problem.
Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = 'text/javascript 'src = 'js/f2.js'> </script>
</Head>
<Body>
</Body>
</Html>

When the page is opened for the first time:

When the page is opened for the second time:

As shown in the preceding example, a browser of a later version such as chrome caches js files, reducing network requests.

Second, the second problem is whether a javascript file is blocked when it is loaded. The book "high-performance Javascript" provides a better solution to this problem: the processing of earlier versions of various browser browsers is that when a javascript file is being loaded, loading of other page files (including other javascript files) will be blocked at the same time, but IE8, Firfox3.5, Safari 4 and Chrome 2 both allow parallel download of javascript files, but unfortunately, during the javascript download process, the download of other resources will still be discarded. Although the download process of javascript scripts does not affect each other, the page must wait until all javascript code is downloaded and executed.

The browser also limits the number of concurrent links under the same domain name. Other parameters are as follows:

Ii. Skills
1. Script location
Because the script will block the download of other resources on the page, we recommend that you put all the <script> labels at the bottom of the <body> label to minimize the impact on the download of the entire page.
2. Merge JavaScript files that can be merged
3. No blocking script
Currently, the most common method is to dynamically load and execute scripts. By using DOM, you can use Javascript to dynamically create all the content in HTML. Basically, the <script> tag is no different from other elements on the page: you can use DOM references to move, delete, and create documents. When the <script> element of a file is added to the page, it does not stop other files from being downloaded and only blocks rendering during the execution phase. Special emphasis: The article "high-performance javascript" states that "the focus of this technology is that the download and execution of files will not block other processes on the page at any time ", this does not mean that it does not block other javascript code during execution, but emphasizes that it will not block other tasks such as downloading other resources.
The specific code is as follows:
Copy codeThe Code is as follows:
Function loadScript (url ){
Var ga = document. createElement ('script ');
Ga. type = 'text/javascript ';
Ga. async = true;
Ga. src = url;
(Document. getElementsByTagName ('head') [0] | document. getElementsByTagName ('body') [0]). appendChild (ga );
}

4. Magical setTimeout ()
Here, I only use the setTimeout () principle. If you are interested, you can go to Chapter 6 of "high-performance javascript. I emphasize that the second parameter of setTimeout is not a precise time, and the second parameter must be run when the javascript thread is idle. With this feature, the following code is simple and can be executed after other js code is executed.
Copy codeThe Code is as follows:
SetTimeout (function (){
// Do some before other javascripe codes had processed
}, 25)

But do not use document in function. write () method, because when the function in setTimeout is executed, the page onload is usually reached, and then the document is executed. write will clear the content of the current page because it will automatically trigger document. open method.
High-performance Javascript
How browsers work: BEHIND THE SCENES OF MODERN WEB BROWSERS
Google Chrome source code analysis [1]: multithreading Model
Javascript asynchronous 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.