The parsing steps of the JavaScript JavaScript learning JS script

Source: Internet
Author: User
Tags blank page

There are two ways to add JavaScript code to the HTML code, that is, to use the <script> tag: embed the page directly and use an external JS file.

When embedding HTML code with the <script> tag, you need to specify its type: type= "Text/javascript". However, in the HTML5 standard <script> label type default is "Text/javascript", you can omit to write, but considering the compatibility of the lower version of the problem, generally add better.

  Embedded JS Code:

The content added to the <script> tag is interpreted from top to bottom. The contents of the page will not be loaded or displayed by the browser until the code inside the interpreter is processed, which means that the page load will be blocked until the JS code is loaded and the execution is complete.

  Reference external JS script:

In the <script> tab, you can use "src" to introduce an external JavaScript file, and when the <script> tag is used to reference an external JS file, the code inside the tag is ignored.

and embedded JS code, when the browser resolves to the <script> tag, the first load external JS file code, and then the interpreter will explain the code, the JS file code is interpreted, the browser page load will be blocked until the import code load execution is complete.

By convention,<script> tags are generally written in the

To solve this problem, you can put the introduction of the external JS file into the <body> DOM structure code, so that the page in the loading process, the first load the DOM structure and then start loading interpretation JS file content, so that the existence of blank pages will be reduced, Also in disguise gives a kind of page to open the feeling quickly.

  Delay Script Defer:

Accordingly, if you do need to write the <script> tag of the extension JS file in

<script type= "Text/javascript" src= "XXXXX" defer= "defer" ></script>

  Async Script Async:

This property is a new property of HTML5, similar to the Defer property, which is a property that alters the behavior of external script execution. Tags <script> when async is set, the external script executes asynchronously, but does not control the order in which several external scripts are executed, so if there is a change in the contents of the DOM in the script, an unexpected error will occur.

<script type= "Text/javascript" src= "XXXXX" async= "Async" ></script>

As a result, there are three ways to execute an external script:

    if async= "Async": The script executes asynchronously relative to the rest of the page (when the page resumes parsing, the script is executed);

    If Async is not used and defer= "defer": The script executes when the page finishes parsing;

    if neither async nor deferis used: The script is read and executed immediately before the browser continues to parse the page.

The parsing steps of the JavaScript JavaScript learning JS script

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.