2nd two ways to invoke JS--using JavaScript in HTML

Source: Internet
Author: User
Tags script tag

I. <script> marks 

The first method is to put <sript></script> directly between the head and script two tags (title below,

    <Head>...    <title>Demo</title>    <Scripttype= "Text/javaScript ">    /*The type attribute is often ignored. Script default is JS, so <script type= "Text/java script" > can be directly abbreviated to do <script>*/    ...    </scipt>    </head>

Embedded JS, may involve some conflict with the HTML judgment. Operators such as <>. Can be overridden by HTML entities. such as &gt; for >,&lt; <.

two. Referencing an outside chain 

Write a good JS file, and then reference, in the JS file, do not need to add <script> tags.

  

<typescript "src=" Xxx.js "></script > //The internal JS code will be ignored. 

It is recommended to use the second method. It has the advantages of convenient maintenance, cache, standardization and so on.

 three. So here's the problem .

The script tag is loaded from top to bottom. The rest of the page is not displayed until the parser <script> element evaluates to the end of all JS code. How do I improve performance?

  1.defer Property Lazy Loading

One method is to take advantage of the defer property of the script element. A surface script executes when it does not affect the construction of the page (only after the HTML content has finished loading). If you include in the

<deferarc= "Xxx*.js"></script>

When two defer exist, they are theoretically executed sequentially.

  2. Asynchronous loading

Another property of script, Async, is similar to defer. Requires that the JS file be downloaded immediately without affecting the construction of the page.

<deferAsync arc= "Xxx*.js"></ Script >

But when there are two async, even theoretically there is no guarantee that the two are sequentially loaded.

  3. The best way

Put <script> to the end of the HTML page (before </body>), and then refer to the outside chain-so that the file can be loaded better and faster.

  4. A further question

When the browser does not support the script, or the browser JS script is disabled, how to make the page smooth degradation?

Add the NoScript code to the last part of the BODY element (this code is not displayed when JS is normal):

< NoScript > < P > This page requires a browser support (enable) JavaScript script. </ P > </ Nosript >

Added

Historically, JS can be written in HTML annotations. It can be considered as the "row form" of JS. <!--JS code//-->, now obsolete.

2nd two ways to invoke JS--using JavaScript in HTML

Related Article

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.