Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q421 What is the difference between writing a JavaScript into <HEAD> </HEAD> and <BODY> </BODY> tags?

irt.org | Knowledge Base | JavaScript | General | Q421 [ previous next ]

Q421 What is the difference between writing a JavaScript into <HEAD> </HEAD> and <BODY> </BODY> tags?

Its a good idea to place all your JavaScript function definitions within the HEAD elements, that way you are 100% certain that they are defined before they are used - especially important if they are invoked via an event handler.

When using JavaScript in a frameset you must place all the JavaScript with the HEAD elements otherwise you cannot guarantee that the frames will be displayed correctly - this is especially true for Microsoft Internet Explorer.

Placing JavaScript code in tables can cause problems on some browsers. The trick here is to make sure the whole of a table row is output from JavaScript rather than just the contents of the table row.

If you output to the document from JavaScript before the BODY tag, then in effect your starting the body of the page earlier in your document than any actual BODY tag in the code. This may cause other elements that should be positioned prior to the BODY tag, e.g. TITLE, META etc, to not be processed correctly.

Apart from that you can place JavaScript almost anywhere in a document.


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.