Parsing JavaScript code should be placed in the HTML code which location is better

Source: Internet
Author: User
Tags cdata

This paper summarizes a variety of ways to place JS code, the need for friends can refer to the following

Where do you place JavaScript code?

Typically, JavaScript code is used in conjunction with HTML code to place JavaScript code anywhere in an HTML document. Where it is placed, however, will have a certain effect on the normal execution of JavaScript code, as described below.

Place in between

It is a common practice to place JavaScript code between the

<!DOCTYPE HTML><HTML>    <Head>        <MetaCharSet= "Utf-8" />        <title></title>        <Scripttype= "Text/javascript">            /*... * * Javascript code * * ..... * */        </Script>    </Head>    <Body>            </Body></HTML>

Place in between <body></body>

There are also some cases where JavaScript code is placed between <body></body>. Consider the following scenario: We have a piece of JavaScript code that needs to manipulate HTML elements. However, since HTML documents are loaded from top to bottom, the HTML element is not loaded and an error (the object does not exist) to avoid JavaScript code manipulating the HTML element, so you need to write this code behind the HTML element, as in the following example:

<!DOCTYPE HTML><HTML>    <Head>        <MetaCharSet= "Utf-8" />        <title></title>    </Head>    <Body>    </Body>    <DivID= "Div1"></Div>    <Scripttype= "Text/javascript">document.getElementById ("Div1"). InnerHTML="Test Text"; </Script></HTML>

But typically, we operate on page elements that are usually driven by events, so this is a rare scenario. In addition, we do not recommend writing JavaScript code outside of

Tips

If the HTML document is declared as XHTML,<script></script> tags must be declared within the CDATA section, XHTML will resolve the <script></script> tag to another XM L tag, the JavaScript code inside may not execute properly. Therefore, using JavaScript in strict XHTML should be declared as in the following example:

<Head>        <MetaCharSet= "Utf-8" />        <title></title>        <Scripttype= "Text/javascript">             < ![cdata[JavaScript Code]]>        </Script>    </Head>

Both of these ways of writing JavaScript code into an HTML document are the way in which JavaScript code is referenced inside an HTML document. In addition to internal references, you can also use external references.

External reference JavaScript Code

Separate the JavaScript code (excluding <script></script> tags) into a single document, named after the JS suffix, such as myscript.js, and in the HTML document <script></ The script> tag uses the SRC attribute to refer to the file:

<Head>        <MetaCharSet= "Utf-8" />        <title></title>        <Scripttype= "Text/javascript"src= "Myscript.js"></Script>    </Head>

After using the externally referenced JavaScript code, the benefits are obvious:
1. Avoid using <!--... in JavaScript code//-->
2. Avoid the use of unsightly CDATA
3. Common JavaScript code can be reused for other HTML documents, as well as for consistent JavaScript code maintenance
4.HTML document is smaller, facilitate search engine included
5. A single JavaScript file can be compressed and encrypted
6. Browsers can cache JavaScript files and reduce broadband usage (usually only once when multiple pages are using a single JavaScript file)
7. Avoid using complex HTML entities, such as direct use of document.write (2>1) without writing document.write (2<1)

The formation of JavaScript code as an external file also increases the load on the server's HTTP requests, which is not a good strategy in the context of extremely high concurrent requests. In addition, when referencing external JS files, the correct path of the file should be noted.

Parsing JavaScript code should be placed in the HTML code which location is better

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.