Do you understand the order in which JavaScript is executed?

Source: Internet
Author: User
Perform

Normally, JavaScript is executed from top to bottom as an explanatory script, but JavaScript also allows nesting in its statements, which is this:

document.write ("<script>alert (t); </SCR" + "ipt>");

Note that:</script> cannot write </script>, it is to be expressed in the form of string concatenation, or there will be grammatical errors, presumably because JavaScript encounters </script> thinks the script is over.

In this case, the order in which the normal scripts and embedded scripts are executed needs to be studied.

b.js:[DOWNLOAD]

Alert ("5");


a.js:[DOWNLOAD]

Alert ("4");
document.write ("<script src=b.js></scr" + "ipt>");
Alert ("6");


test.html:[DOWNLOAD]

<script src=a.js></script>
<script>
Alert ("1");
document.write ("<script src=b.js></scr" + "ipt>");
document.write ("<script>alert (" 3 ") </scr" + "ipt>");
Alert ("2");
</script>

To perform test.html, you can see the order in which the prints are: 4,6,5,1,3,2,5

can also do some related tests, concluded that:
1. Different code blocks of the same level, the order of execution between code blocks is from top to bottom;
2. In the case of embedding code in the code, execute the upper-layer code block, then execute the child code block; The embedded code in the code refers to the introduction of a file into another file, rather than all the code that is typed in document.write form.



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.