" 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

Where to put javascript code

青灯夜游
青灯夜游Original
2021-06-11 17:57:074690browse

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.

Where to put javascript code

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

<html>
<head>
<script type="text/javascript">
<![CDATA[
JavaScript 代码
]]>
</script>
</head>
....

The above two ways of writing JavaScript code into HTML documents are ways of referencing JavaScript code inside the HTML document. In addition to internal references, external references can also be used.

External reference JavaScript code (placed in an external file with the extension ".js")

Replace the JavaScript code (excluding 3f1c4e4b6b16bbbd69b2ee476dc4f83a3fa3f474cbb4b6d948eebecb1be5dde4 tag) forms a separate document and is named with the js suffix, such as myscript.js, and uses the src attribute in the HTML document3f1c4e4b6b16bbbd69b2ee476dc4f83a2cacc6d41bbb37262a98f745aa00fbf0 tag to reference the file:

<html>
<head>
<script type="text/javascript" src="myscript.js"></script>
</head>
....

After using external reference JavaScript code, the benefits are obvious:

1. Avoid using 7abb30cfb0195b52eebaf7024287f82e
in JavaScript code 2. Avoid using ugly CDATA
3. Public JavaScript code can be reused in other HTML documents, which is also conducive to the unified maintenance of JavaScript code
4. HTML documents are smaller, which is conducive to search engine inclusion
5 .Can compress and encrypt a single JavaScript file
6. The browser can cache JavaScript files to reduce broadband usage (when multiple pages use a JavaScript file at the same time, it usually only needs to be downloaded once)
7. Avoid complex usage HTML entities, for example, you can use document.write(2>1) directly without writing document.write(2<1)

Forming JavaScript code into an external file will also increase the HTTP request burden on the server. This is not a good strategy in an environment with extremely high concurrent requests. In addition, when referencing external js files, you need to pay attention to the correct path of the file.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of Where to put javascript code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn