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.