The two methods that are usually most familiar to you are:
- Write <script type= "Text/javascript" >JS code </script> directly in the page.
- Introduce external files in the page <script src= "Xx.js" ></script>.
Here are a few more pages to introduce JS code:
- The introduction of external JS in JS, through the Document.wirite ("SCR" + "IPT src= ' xx.js ' ></scr" + "IPT" >. At this point:<script></script> is disassembled as "<SCR" + "IPT ... </scr" + "IPT" >, if not disassembled, the browser may turn the parent JS fragment off, resulting in an error.
- In JS reference other JS fragments, document.write ("<SCR" + "Ipt>alert (XXX) </scr" + "ipt>");
- Added via DOM: Var scr=document.createelement ("script"); Scr.src= "Xxx.js";
Below the test, different ways to introduce JS, the time sequence of loading:
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <MetaCharSet= "Utf-8">5 <title></title>6 <Scriptsrc= "2.js"type= "Text/javascript"></Script>7 <Scripttype= "Text/javascript">8 document.write ("<SCR"+"Ipt>alert (4) </SCR"+"ipt>");//introduced in the fourth way9 document.write ("<SCR"+"IPT src= ' 3.js ' ></SCR"+"ipt>");//The Third way to introduceTen Alert (1); One document.write ("<SCR"+"IPT src= ' 3.js ' ></SCR"+"ipt>"); A Alert (5); - </Script> - </Head> the <Body> - </Body> - </HTML>
Execution order: 2-4-1-5-3-3
After the third and fourth exchange sequences:
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <MetaCharSet= "Utf-8">5 <title></title>6 <Scriptsrc= "2.js"type= "Text/javascript"></Script>7 <Scripttype= "Text/javascript">8 document.write ("<SCR"+"IPT src= ' 3.js ' ></SCR"+"ipt>");//The Third way to introduce9 document.write ("<SCR"+"Ipt>alert (4) </SCR"+"ipt>");//introduced in the fourth wayTen Alert (1); One document.write ("<SCR"+"IPT src= ' 3.js ' ></SCR"+"ipt>"); A Alert (5); - </Script> - </Head> the <Body> - </Body> - </HTML>
Execution order: 2-1-5-3-4-3
You can see the Third Way: Document.wirite ("SCR" + "IPT src= ' xx.js ' ></scr" + "IPT" > Introduce JS code to execute the JS text that will be written later and directly.
At the same time, the fourth way to introduce JS execution and write directly to the text is executed sequentially.
In summary, the commonly used several ways to introduce JS code:
- Write <script type= "Text/javascript" >JS code </script> directly in the page.
- Introduce external files in the page <script src= "Xx.js" ></script>.
- The introduction of external JS in JS, through the Document.wirite ("SCR" + "IPT src= ' xx.js ' ></scr" + "IPT" >.
- In JS, refer to other JS fragments, document.write ("<SCR" + "Ipt>alert (XXX) </scr" + "ipt>").
- Added via DOM: Var scr=document.createelement ("script"); Scr.src= "Xxx.js";
Several methods of introducing JS in the page