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.