Several methods of introducing JS in the page

Source: Internet
Author: User

The two methods that are usually most familiar to you are:

    1. Write <script type= "Text/javascript" >JS code </script> directly in the page.
    2. Introduce external files in the page <script src= "Xx.js" ></script>.

Here are a few more pages to introduce JS code:

    1. 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.
    2. In JS reference other JS fragments, document.write ("<SCR" + "Ipt>alert (XXX) </scr" + "ipt>");
    3. 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:

    1. Write <script type= "Text/javascript" >JS code </script> directly in the page.
    2. Introduce external files in the page <script src= "Xx.js" ></script>.
    3. The introduction of external JS in JS, through the Document.wirite ("SCR" + "IPT src= ' xx.js ' ></scr" + "IPT" >.
    4. In JS, refer to other JS fragments, document.write ("<SCR" + "Ipt>alert (XXX) </scr" + "ipt>").
    5. Added via DOM: Var scr=document.createelement ("script"); Scr.src= "Xxx.js";

Several methods of introducing JS in the page

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.