Home  >  Article  >  Web Front-end  >  Where does the javascript script appear?

Where does the javascript script appear?

藏色散人
藏色散人Original
2021-07-03 09:44:543627browse

Javascript scripts can appear in two locations, namely: 1. In the HTML body part, the js at this location will be executed when the page is loaded; 2. In the HTML head part, the js at this location js will be executed when called.

Where does the javascript script appear?

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

The location of JS script in Html

Where to put JavaScript script?

The JavaScripts in the HTML body part will be loaded when the page is loaded be executed.

JavaScripts in the HTML head section will be executed when called.

————————————————————————————

Where should JavaScript be placed

The JavaScripts in the page will be executed immediately when the browser loads the page. We don’t want this to always be the case. Sometimes we want a script to be executed when the page is loaded, and sometimes we want it to be executed when the user Execute the script when an event is triggered.

Scripts in the head section: Scripts that need to be called to be executed or scripts that are triggered by events are placed in the head section of HTML. When you place a script in the head section, you ensure that the script is loaded before any calls are made.

<html>
<head>
<script type=”text/javascript”>
….
</script>
</head>

Scripts in the body section: Scripts that are executed when the page is loaded are placed in the body section of the HTML. Scripts placed in the body section are usually used to generate the content of the page.

<html>
<head>
</head>
<body>
<script type=”text/javascript”>
….
</script>
</body>

The body and head parts can have scripts at the same time: You can put countless scripts in the file, so your file can have scripts in both the body and head parts.

<html>
<head>
<script type=”text/javascript”>
….
</script>
</head>


——————————————————————————————–

External script Use

Sometimes you may want to run the same script on several pages without having to write the code repeatedly in each page. This is when you need to use external scripts. You can write the script in an external file and save it with a .js extension.

Note: Do not include the tag 3f1c4e4b6b16bbbd69b2ee476dc4f83a in external script files!

When using external scripts, replace the tag 3f1c4e4b6b16bbbd69b2ee476dc4f83a Just point the "src" attribute value to the corresponding .js file:

<html>
<head>
<script src=”xxx.js”></script>
</head>
<body>
</body>
</html>

Recommended study: "javascript Advanced Tutorial"

The above is the detailed content of Where does the javascript script appear?. 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