The difference between placing Javascript in the head and body (W3C recommends placing it in the head label)

Source: Internet
Author: User
Where can JavaScript scripts be stored?

The javascripts in the HTML body section is executed when the page is loaded.
The javascripts in the HTML head section is executed only when it is called.

--------------------------
Where should JavaScript be stored?
The javascripts in the page will be executed immediately when the browser loads the page. We do not want to always do this. Sometimes we want to execute a script when loading the page, sometimes we want to execute the script when the user triggers an event.

Scripts in the head section: scripts that need to be called before execution or scripts that trigger event execution are placed in the head section of HTML. When you place the script in the head part, you can ensure that the script is loaded before any call.

<HTML>
<Head>
<SCRIPT type = "text/JavaScript">
....
</SCRIPT>
</Head>

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

<HTML>
<Head>
</Head>
<Body>
<SCRIPT type = "text/JavaScript">
....
</SCRIPT>
</Body>

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

<HTML>
<Head>
<SCRIPT type = "text/JavaScript">
....
</SCRIPT>
</Head>
<Body>
<SCRIPT type = "text/JavaScript">
....
</SCRIPT>
</Body>

----------------------------
Use of external scripts

Sometimes you may want to run the same script program on several pages without repeatedly writing the code on each page. In this case, you need to use external scripts. You can write the script in an external file and save it in a file with the extension. js.

Note: do not include a tag in an external script file <SCRIPT>!

When using an external script, point the "src" attribute value of the Flag <SCRIPT> to the corresponding. js file:

<HTML>
<Head>
<SCRIPT src = "XXX. js"> </SCRIPT>
</Head>
<Body>
</Body>
</Html>

Note: Remember to place the script tag pointing to the external script file where you usually write the script!

 

1: place the Javascript identifier

2: Put the head of the HTML object, which is run before the page is loaded. Put it into the body, then the JavaScript code will be run only after loading ~~~

So execute the command first in the head.

3: If a function is used, there is no difference. If <SCRIPT> alert (1) </SCRIPT> is related to the position where the head is loaded, then alert is required to load the sorted alert in the body.

Client scripts are generally placed between If the browser does not support this script, the browser will
Skip it, do not execute it, and do not submit an error message

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.