No, JS should always be executed in the body tag after all of the mark-up..
Like so:
*DOM-independent*: JavaScript that never references 'document' or conditionally checks if the document or parts of the document are loaded, and does not error if parts of the document are never loaded.HTML Code:<!doctype html> <html> <head> <!-- title, meta, css, and *DOM-independent* JavaScript --> </head> <body> <!-- all mark-up for browser display --> <script> //My *DOM-dependent* JavaScript. <script> </body> </html>
*DOM-dependent*: JavaScript that references the browser's 'document' variable or anything relating to the Document Object Model'
Since my example uses document.getElementsByClassName('class'), I am assuming there is an element with the class of 'class'. If used in the <head> section or anywhere before '<div class="class">' is seen (the browser reads from top to bottom), then it will be an empty list- as it wont see the elements if it is used before the elements are there.


Reply With Quote
Bookmarks