Where should JavaScript code be placed in HTML code ?, Javascripthtml
Where can I place JavaScript code?
Generally, JavaScript code is used together with HTML code. You can place JavaScript code anywhere in the HTML document. However, the place where JavaScript code is stored may affect the normal execution of JavaScript code, as described below.
Placed between
It is a common practice to place JavaScript code between Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
......
JavaScript code
......
</Script>
</Head>
....
Placed between <body> </body>
In some cases, JavaScript code is placed between <body> </body>. Imagine the following scenario: we have a piece of JavaScript code that requires operations on HTML elements. However, because HTML documents are loaded from top to bottom by the browser, an error is reported to prevent HTML elements from being loaded when JavaScript code is used to operate on HTML elements (the object does not exist ), therefore, you need to write this code to the end of the HTML element. The example is as follows:
Copy codeThe Code is as follows:
<Html>
<Head>
</Head>
<Body>
</Body>
<Div id = "div1"> </div>
<Script type = "text/javascript">
Document. getElementById ("div1"). innerHTML = "test text ";
</Script>
</Html>
However, in general, the page elements are driven by events, so this is rare. In addition, we do not recommend writing JavaScript code outside
Prompt
If the HTML document is declared as XHTML, the <script> </script> tag must be declared in the CDATA section; otherwise, the <script> </script> tag is resolved as another XML tag, the JavaScript code in it may not be executed normally. Therefore, using JavaScript in strict XHTML should be declared as follows:
Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
<! [CDATA [
JavaScript code
]>
</Script>
</Head>
....
The above two methods of writing JavaScript code to the HTML document are the methods of referencing JavaScript code in the HTML document. In addition to internal references, you can also use external references.
External Reference of JavaScript code
Separate the JavaScript code (excluding the <script> </script> tag) into a document and name it with the js suffix, such as myscript. and use the src attribute in the HTML document <script> </script> label to reference the file:
Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript" src = "myscript. js"> </script>
</Head>
....
After using external reference JavaScript code, the benefits are obvious:
1. Avoid using it in JavaScript code <! --... // -->
2. Avoid using ugly CDATA
3. Common JavaScript code can be reused in other HTML documents, which facilitates unified maintenance of JavaScript code.
4. Smaller HTML documents facilitate search engine Indexing
5. Individual JavaScript files can be compressed and encrypted.
6. the browser can cache JavaScript files to reduce bandwidth usage (when multiple pages use one JavaScript file at the same time, it usually only needs to be downloaded once)
7. Avoid using complex HTML entities. For example, you can directly use document. write (2> 1) without writing document. write (2 <1)
Forming JavaScript code as an external file also increases the HTTP request burden on the server. This is not a good strategy in a highly concurrent request environment. In addition, when referencing an external js file, pay attention to the correct path of the file.
Which html element can I place javascript code in? <Head> <script> If there are many types, you can write them in the *. js file and call the <script type = "javascript" src = "Storage path"> </script> file in
Where should javascript code be written in jsp?
Written outside of <%>, but note that,
If you call a function when loading a page somewhere and the function needs to operate on HTML elements, you need to put javascript code after this element, because there must be an element before you can operate on the element, otherwise, the expected results will not be available.
For example:
<Div id = "haha"> </div>
<Script type = "text/javascript">
Document. getElementById ("haha"). innerHTML = "loading ...";
</Script>
In the script, to operate on haha, put it behind the div whose id is haha.