Simple. Where should your javascript go in an html document. In the head? or at the bottom of the page? And what determins the postioning?
Asked
Active
Viewed 66 times
2 Answers
1
You can put it anywhere you want. But keep in mind that the javascript will be imidiatly executed when the browser finds it. This can lead to blocking situations. So it's normaly found in the end of the HTML just before the body to run after all elements get loaded.
Although jQuery fixes that with the famous:
$(document).ready(function(){ ...

fmsf
- 36,317
- 49
- 147
- 195
-
Or $(function() { ... } – Undefined Feb 08 '13 at 09:05
-
This is somewhat incorrect (even though a similar answer is accepted in the dupe). It's normally found just before the closing body to prevent the script from being blocking. Having a script in your head means that the parsing engine will not continue parsing the document until the script is downloaded and has finished executing. You *can* get around it in newer browsers using the `async` attribute. – Andy E Feb 08 '13 at 09:08
-
@AndyE I agree, updated the answer – fmsf Feb 08 '13 at 09:29
0
Well it all depends on what the scripts are doing.
personally i put the references for JQuery JqueryUI and JQuery Validation in the Head, and then all other custom scripts after the body.
the reason for this is because i would want my Jquery Items to load before the page has finished loading.
All of my custom code is usually put after the body tag because these can load after the page has finished rendering.

Qpirate
- 2,078
- 1
- 29
- 41