Differences between JavaScript code in head and body

Source: Internet
Author: User

So what is the difference? Let's take a look at an example:
A list of level-2 cascade dynamic drop-down lists with level-1 category id = "vSort0 ". Copy codeThe Code is as follows: Function changelocation (id)
{............}
</Head>
<Body> <select class = "input1" id = "vSort0" name = "vSort0" onChange = "changelocation (document. form4.vSort0. options [document. form4.vSort0. selectedIndex]. value); "style =" width: 100px; "> ...... Omitted ...... </Select>
..................
</Body>

There is a js script:Copy codeThe Code is as follows: <script LANGUAGE = "JavaScript">
Changelocation (document. form4.vSort0. options [document. form4.vSort0. selectedIndex]. value); // initialize the second-level category of the first level-1 category. The second-level category of the first level-1 category is not displayed after the page is loaded. Select back to display. Pass the value of a level-1 category to the changelocation () function to generate a list of level-2 categories.
</Script>

So should I put this js script in the head or in the body?
The answer is not only to put it in the body, but also to put it behind the list box defining id = 'vsort0 ', because this js script contains document. form4.vSort0. selectedIndex. If you put it in the head or the id of the body is 'vsort0 ', the page is loaded and the code is executed in sequence. Then, the js code is executed and vSort0 is undefind ), this js is useless.
Why do we often see that it is okay to put JavaScript scripts in the head? Yes!
It is because the js Code in the head contains onclick and other events that pass variables to the function.
This tells us that if we want to define a global object that is related to a button (and so on) on the page, we must put it into the body. The principle is obvious: if you put the head in, when the page loads the head part, the button (and so on) has not been defined (it can be said that it has not been loaded yet, because the loading process is the process of executing the Code, including the definition), you can only get an undefind.

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.