What is the best place for JavaScript code to be placed in HTML code? _javascript Tips

Source: Internet
Author: User
Tags cdata

Where do I put the JavaScript code?

Typically, JavaScript code is used with HTML code, and JavaScript code can be placed anywhere in an HTML document. However, the place will have a certain effect on the normal execution of JavaScript code, as described below.

Placed between

It is a common practice to place JavaScript code between the

Copy Code code as follows:

<script type= "Text/javascript" >
......
JavaScript Code
......
</script>
....

Placed between <body></body>

There are also parts of the situation where JavaScript code is placed between <body></body>. Imagine a situation where we have a JavaScript code that needs to manipulate HTML elements. However, because HTML documents are loaded from top to bottom in the browser, and to avoid JavaScript code manipulating HTML elements, the HTML element is not loaded and the error is not present (the object does not exist), so you need to write this code behind the HTML element, as shown in the following example:

Copy Code code as follows:

<body>
</body>
<div id= "Div1" ></div>
<script type= "Text/javascript" >
document.getElementById ("Div1"). Innerhtml= "Test text";
</script>

Often, however, we manipulate page elements to be driven by events, so this is rarely the case. In addition, we do not recommend that you write JavaScript code outside of

Tips

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

Copy Code code as follows:

<script type= "Text/javascript" >
<! [cdata[
JavaScript Code
]]>
</script>
....

Both of these ways to write JavaScript code into an HTML document are the ways in which JavaScript code is referenced within an HTML document. In addition to internal references, you can use an external reference method.

External referencing JavaScript code

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

Copy Code code as follows:

<script type= "Text/javascript" src= "Myscript.js" ></script>
....

After using the external reference 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, and also for unified maintenance of JavaScript code
4.HTML document smaller, for search engine included
5. Can compress, encrypt a single JavaScript file
6. Browsers can cache JavaScript files, reducing the use of broadband (when multiple pages use a JavaScript file at the same time, usually only download once)
7. Avoid the use of complex HTML entities, such as direct use of document.write (2>1) without the need to write document.write (2<1)

The formation of JavaScript code as an external file also increases the server's HTTP request burden, which is not a good strategy in an ultra-high concurrency request environment. In addition, when referencing external JS files, you need to pay attention to the correct path of the file.

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.