The difference between the JavaScript code and the HTML code in different locations

Source: Internet
Author: User
Tags cdata

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.

1, placed 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>

2, placed 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.

3. External reference JavaScript Code

Separate the JavaScript code (excluding the label) into a single document, named after the JS suffix, such as myscript.js, and use the SRC attribute in the HTML document tag 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 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 the use of complex HTML entities, such as the ability to directly use document.write (2>1) without writing document.write (2<1) to form the JavaScript code as an external file, but also increase the server's HTTP request burden, This is not a good strategy in an environment of extremely high concurrent requests. In addition, when referencing external JS files, the correct path of the file should be noted.

The difference between the JavaScript code and the HTML code in different locations

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.