Possible Duplicate:
Where is the best place to put <script> tags in HTML markup?
Where should I put javascript code?
<-- Here?
</body>
<-- Here?
</html>
<-- Here?
I've always used above </body>
just wanted to know if this was "the best"
Possible Duplicate:
Where is the best place to put <script> tags in HTML markup?
Where should I put javascript code?
<-- Here?
</body>
<-- Here?
</html>
<-- Here?
I've always used above </body>
just wanted to know if this was "the best"
If the JavaScript needs to be ready to go before the entire page loads, you put it between <head></head>
. If the JavaScript needs to be available right before the user encounters something on the page, you can put it prior to the element in the <body></body>
(although people hardly do that for code readability). If the JavaScript just does some enhancements or provides tracking information (it's not a necessity), you can put it right before </body>
.
Concensus seems to be that there are two sensible places to put scripts:
In the <head>
section:
Just before the </body>
tag:
You need to decide what your priorities are and what your specific needs are. Personally, I'll put anything absolutely critical to the proper functioning of the page in <head>
and anything that is progressive enhancement at the end.
Really depends on what you are trying to do.
General rule of thumb would have you put your javaScript code in a seperate file and link that script with something like this
<script type="text/javascript" src="your/source/here.js"></script>
Then again, having your script inline within the page, you can have it where you want. Within the head
section of the page, it loads beforehand, and therefore can pre-process if needed. If you have it let's say during the body
portion of the page, it will load with the normal flow of the page.
Having everything on the same file, I would have the script in the head
and, if needed, call functions afterwards in the body
.
Hope this helps.
Keep in mind that the main goal should be readability and re-usability of your code. Therefore, whatever makes this happen in the most efficient way, I would have to say is the best way.
Putting <script>
after </body>
or </html>
would be invalid, and I'm not sure what it would accomplish. Some scripts, for analytics services for example, are recommended to be loaded at the end of body (just before </body>
) because they load synchronously and may hang the page.
Don't think it's nearly as much about where in the code, it's how you handle checking if you can run code when it triggers. i.e. onload or onready.
HTML5 boilerplate promotes having most if not all scripts as the last elements in the body, but I have yet to ever find time to benchmark this vs having it in the head.
JavaScript can be added anywhere. Just a basic example below as you are learning things.
<html>
<head>
----- Your Javascript here -----
</head>
<body>
</body>
</html>
Inside </html>
Inside <body>
That said, from there it's sometimes just preference. Many say at the end of the doc because there's a supposed speed increase. If it's there, it's minuscule.
Its better if you put javascript code inside <head> // javascript code here </head>
because head section loads before your page appears.