" tag pair to ensure that the js code has been loaded before the script needs to be used; 2. Place it in the "
" tag is centered; 3. Place it in an external file of ".js" and introduce it with the script tag."/> " tag pair to ensure that the js code has been loaded before the script needs to be used; 2. Place it in the " " tag is centered; 3. Place it in an external file of ".js" and introduce it with the script tag.">Home > Article > Web Front-end > Where to put javascript code
The location of the javascript code: 1. Place it in the "93f0f5c25f18dab9d176bd4f6de5d30e9c3bca370b5104690d9ef395f2c5f8d1" tag pair to ensure that the js code has been loaded before the script needs to be used; 2. Place it in "6c04bd5ca3fcae76e30b72ad730ca86d36cc49f0c466276486e50c850b7e4956" tag is centered; 3. Place it in an external file of ".js" and introduce it with the script tag.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Where to put JavaScript code?
Normally, JavaScript code is used together with HTML code, and JavaScript code can be placed anywhere in the HTML document. However, where it is placed will have a certain impact on the normal execution of JavaScript code, as detailed below.
Place between 93f0f5c25f18dab9d176bd4f6de5d30e9c3bca370b5104690d9ef395f2c5f8d1
Place the JavaScript code between the 93f0f5c25f18dab9d176bd4f6de5d30e9c3bca370b5104690d9ef395f2c5f8d1 tags of the HTML document time is a common practice. Since HTML documents are loaded by the browser from top to bottom, placing JavaScript code between the 93f0f5c25f18dab9d176bd4f6de5d30e9c3bca370b5104690d9ef395f2c5f8d1 tags ensures that it has been loaded before the script needs to be used:
<html> <head> <script type="text/javascript"> …… JavaScript 代码 …… </script> </head> ....
Placed between 6c04bd5ca3fcae76e30b72ad730ca86d36cc49f0c466276486e50c850b7e4956
There are also some cases where JavaScript code is placed between 6c04bd5ca3fcae76e30b72ad730ca86d36cc49f0c466276486e50c850b7e4956 of. Imagine the following situation: we have a piece of JavaScript code that needs to operate on HTML elements. However, since the HTML document is loaded sequentially from top to bottom by the browser, in order to prevent the JavaScript code from operating the HTML element and reporting an error (the object does not exist) before the HTML element is loaded, this code needs to be written to the HTML element. Later, the example is as follows:
<html> <head> </head> <body> </body> <div id="div1"></div> <script type="text/javascript"> document.getElementById("div1").innerHTML="测试文字"; </script> </html>
But usually, our operations on page elements are generally driven by events, so the above situation is rare. In addition, we do not recommend writing JavaScript code outside of 100db36a723c770d327fc0aef2ce13b173a6ac4ed44ffec12cee46588e518a5e.
Tip
If the HTML document is declared as XHTML, the 3f1c4e4b6b16bbbd69b2ee476dc4f83a2cacc6d41bbb37262a98f745aa00fbf0 tag must be declared within the CDATA section, otherwise XHTML will convert