Can javascript be written in the body?

青灯夜游
Release: 2023-01-07 11:44:08
Original
5954 people have browsed it

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 page;

  • Html The webpage's

    ;

##Javascript is written in

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>
Copy after login

Javascript is written in

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
of HTML.

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

Javascript is written after

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

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 →→behind body


1. The javascript code is written in :

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 :

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 :

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template