Put the JavaScript file in the head
The standard approach is to put JavaScript files inside the head tag.
<
head
>
<
script
type
=
"
text/javascript
"
src
=
"
dreamdu.js
"
>
</
script
>
</
head
>
- If the browser does not support JavaScript, the contents of the script tag are ignored and can be avoided
<!-- ... //-->
(internal references).
- When JavaScript has any HTML entities, there is no need to use CDATA tags (because the JavaScript code is not directly in the HTML file).
- JavaScript files must be used
js
as file suffixes.
Put the JavaScript file in front of the body
To prevent web pages from loading slowly, you can also put non-critical javascript at the bottom of the page, such as the following code.
<
script
type
=
"
text/javascript
"
src
=
"
dreamdu.js
"
>
</
script
>
</
body
>
</
html
>
Usually JavaScript files can be loaded into any part of the Web page using the script tag, but the standard way is to load it inside the head tag.
The contents of a JavaScript file
JavaScript files can contain any JavaScript code, such as in a dreamdu.js file
document
.
write
(
"
www.dreamdu.com
"
)
;
document
.
write
(
"
梦之都
"
)
;
Benefits of external loading of JavaScript files
- Avoid
<!-- ... //-->
the use of hacker technology.
- Avoid using CDATA.
- Unified definition of JavaScript code, easy to view, easy to maintain.
- Make your code more secure, and you can compress and encrypt a single JavaScript file.
- Browsers can cache JavaScript files and reduce broadband usage (usually only one download at a time when multiple pages are using a single JavaScript file).
Considerations for external loading of JavaScript files
- Instead of dividing JavaScript into multiple files, multiple files increase the burden on the server and increase the server's HTTP requests.
- A JavaScript file also increases the HTTP request.
- Why do some Web sites have a string of numbers behind their JavaScript files? Like what:
<
script
type
=
"
text/javascript
"
src
=
"
dreamdu.js?1217128319
"
>
</
script
>
Several methods of html-html link JavaScript