I read that you should define your JavaScript functions in the <head> tag, but how does the location of the <script> (whether in the <head>, <body>, or any other tag) affect a JavaScript function.
Specifically, how does it affect the scope of the function and where you can call it from?
Telling people to add
<SCRIPT>only in the head sounds like a reasonable thing to do, but as others have said there are many reasons why this isn’t recommended or even practical – mainly speed and the way that HTML pages are generated dynamically.This is what the HTML 4 spec says :
And some sample HTML. Doesn’t it look pretty all formatted here 🙂
And something to look forward to in HTML 5 :
New async attribute in
<SCRIPT>: