JS statement execution order in Javascript

Source: Internet
Author: User

If we have embedded in the HTML <script> JS coding </script> as JS block of code (in fact, JS does not have a block of code meaning, if really want to think of code block that also should be divided by function block), Here is the JS Code execution order:


Here reproduced JS execution sequence summary, please refer to http://www.3lian.com/edu/2014/04-07/139469.html

Step 1. Read in the first block of code <script> JS Coding </script>, in the order in which they are declared in HTML.

  Step 2. Do grammar analysis, parsing the basic syntax format, error is reported grammatical errors (such as parentheses mismatch, etc.), and jump to step5.

  Step 3. The var variable and the function definition are "precompiled" (never error, because only the correct declaration is parsed), that is, these var variables and function will be first preprocessed, that is, stored in memory, but the variable is only declared, and is not assigned a value, Any block of code underneath it can execute calling functions and variables.

  Step 4. Execute code snippet, error (such as variable undefined), after error, if there is no catch then this code execution end, the variable after the error is not assigned value

  Step 5. If there is a next code snippet <script> JS coding </script>, read the next code block and repeat Step2.

  step6. End.

Order of execution based on HTML document flow

The JS code that needs to be executed before the page element is rendered should be placed in the <script> code block in the <body> front.

Instead, after the page element is loaded, JS is placed behind the </body> element,

The function that is registered with the OnLoad event of the body tag is executed at the end. Also keep in mind that Window.onload is the OnLoad event registered on the body, so if we

The body declares window.onload = function () {}; It will not work because it is overwritten by the onload in the body, however, if we

Window.onload The block of code that is placed after the body, then he overwrites the OnLoad event registered in the body, which is relative to $ (document). Ready (function ()

{}) to be executed later, jquery is run after the DOM tree has been loaded, that is, the label is in place, and it is not as window.onload requires all resources to be loaded.

You want to get the height or width of an element, as long as the element is loaded, but the display must not be none

<! doctype html> 


JS statement execution order in Javascript

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.