Home  >  Article  >  Web Front-end  >  Can javascript be placed in the head?

Can javascript be placed in the head?

藏色散人
藏色散人Original
2021-10-14 14:26:213348browse

Javascript can be placed in the head, and when we place the script in the head section, we can ensure that the javascript script is loaded before any call.

Can javascript be placed in the head?

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

Can javascript be placed in the head?

js files can be placed in the head tag.

If the js code does not contain search information, it can be placed anywhere. However, if it contains the information to be searched, in order to better perform the search effect, it is best to place it Search behind information.

Where can 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 It is executed when the page loads, and sometimes we want to execute the script when the user triggers an event.

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 the body and head parts at the same time Script exists.

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


Recommended study: "javascript basic tutorial"

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