" tag pair through the script tag. When the browser loads the Body part of the web page, it will execute the Javascript statement in it, and the output content after execution will be displayed on the web page."/> " tag pair through the script tag. When the browser loads the Body part of the web page, it will execute the Javascript statement in it, and the output content after execution will be displayed on the web page.">

Home  >  Article  >  Web Front-end  >  Can javascript be written in the body?

Can javascript be written in the body?

青灯夜游
青灯夜游Original
2021-07-19 13:55:375930browse

Javascript can be written in the body. Write the Javascript code in the "" tag pair through the script tag. When the browser loads the Body part of the web page, it will execute the Javascript statement in it, and the output content after execution will be displayed on the web page.

Can javascript be written in the body?

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

The placement location of Javascript:

  • Html web page6c04bd5ca3fcae76e30b72ad730ca86d36cc49f0c466276486e50c850b7e4956;

  • Html The webpage's 93f0f5c25f18dab9d176bd4f6de5d30e9c3bca370b5104690d9ef395f2c5f8d1;

##Javascript is written in6c04bd5ca3fcae76e30b72ad730ca86d36cc49f0c466276486e50c850b7e4956

When the browser loads the Body part of the web page, it executes the Javascript statements in it, and the output content after execution is displayed on the web page.

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

Javascript is written in 93f0f5c25f18dab9d176bd4f6de5d30e9c3bca370b5104690d9ef395f2c5f8d1

Sometimes it is not necessary to run Javascript as soon as the HTML is loaded, but when the user clicks When an object in HTML triggers an event, Javascript needs to be called. At this time, such Javascript is usually placed in 93f0f5c25f18dab9d176bd4f6de5d30e9c3bca370b5104690d9ef395f2c5f8d1 of HTML.

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

Javascript is written after 6c04bd5ca3fcae76e30b72ad730ca86d

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

The difference between js code written after body, head and body

The computer reads the code The order is read from top to bottom. The order in the html file is 93f0f5c25f18dab9d176bd4f6de5d30e→6c04bd5ca3fcae76e30b72ad730ca86d→behind body


1. The javascript code is written in 93f0f5c25f18dab9d176bd4f6de5d30e:

Since the body of the web page has not yet been loaded at this time, it is suitable to put some custom functions that are not executed immediately. Statements that are executed immediately are likely to cause errors (depending on the browser)

2. The javascript code is written in 6c04bd5ca3fcae76e30b72ad730ca86d:

You can put functions or statements for immediate execution here, but if you need to interact with web page elements (such as getting the value of a certain tag or giving a certain tag assignment), the Javascript code must be behind the tag

3. The javascript code is written under the 6c04bd5ca3fcae76e30b72ad730ca86d:

At this time, the entire web page has been loaded, so it is most suitable to put it here Commands that need to be executed immediately, and custom functions and the like are not suitable.

[Recommended learning:

javascript advanced tutorial]

The above is the detailed content of Can javascript be written in the body?. 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