The difference between the head and body of JavaScript (the proposal is placed in the head tag)

Source: Internet
Author: User
Tags script tag

Where JavaScript scripts are placed

The javascripts in the HTML body section is executed when the page is loaded.

The javascripts in the HTML Head section is executed when it is called.

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

Scripts in the head section: scripts that need to be invoked to execute or events that trigger execution are placed in the head section of the HTML. When you put the script in the Head section, you can ensure that the script is loaded before any calls.

 

Script in the body section: the script that executes when the page is loaded is placed in the body part of the HTML. The script placed in the body section is often used to generate the contents of the page.

 

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

 

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

Use of external scripts

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

Note: Do not include the tag in the external script file <script>!  

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

 

Note: Remember that this script tag that points to an external script file is placed where you would normally write scripts!

1: Place the JavaScript identifier in the

2: Put the HTML head, is the page loaded before the run, put into the body, then load the code to run JavaScript ~ ~ ~

So the head inside is executed first.

3: If there is no difference in function if the <script>alert (1) </script> will follow the position of a matter of the head loaded on the alert one is to follow the body to load sequential alert   

Client script is typically placed between

If the browser does not support this script, then the browser will

Skipping it, not executing it, and not making an error message


The difference between the head and body of JavaScript (the proposal is placed in the head tag)

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.