Normally, JavaScript, as an interpretive script, is executed sequentially from top to bottom, but JavaScript also allows nesting in its statements, which is like this:
document.write("<script>alert(t );</scr"+"ipt>");
Note: </script> cannot be written as </script> and must be expressed in the form of string concatenation, otherwise there will be syntax errors, probably because JavaScript encounters </ script> assumes that the script is over.
In this case, the execution order of normal scripts and embedded scripts 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>
Executing test.html, you can see that the printing order is: 4,6,5,1,3,2,5.
You can also do some related tests and the conclusion is:
1. Different code blocks at the same level, the execution order between code blocks is from top to bottom;
2. When the code is embedded in the code, the upper code block is executed first, and then the sub-code block is executed; the embedded code in the code refers to the introduction of one file into another file, not all the codes typed in the document.write form.