Does javascript have to be in the head tags?
I believe javascript can be anywhere (almost), but I almost always see it in between <head></head>
. I am using jquery and wanted to know if it has to be in the head tags for some reason or if will break something if I move it. Thank you.
EDIT: Why is it almost always in the head tags?
No, it can be anywhere. In fact, it’s sometimes a good idea to put it at the bottom of the document. For an explanation why, see http://developer.yahoo.com/performance/rules.html#js_bottom.
JavaScript is executed wherever it is found in the document. If you place inline JavaScript in the body, it will be executed when the browser comes to it. If you're using $(document).ready(...)
to execute things, then the positioning shouldn't matter. Otherwise, you may find corner cases where it matters. In general, it does not matter. Scripts end up in the head
tag mostly out of tradition.