Thinking Outside the <head>!

Apart from being nestled inside <script> tags, JavaScript can be put in two other places to run in a Web page. It can either be in a completely separate file altogether and the JavaScript code linked to the Web page using HTML (in a similar way to how images are separate files but appear as part of the main Web page) or be inline in the <body> tag inside other appropriate HTML elements. Both methods have their own advantages and disadvantages, and by the end of this chapter, you'll have a pretty good idea of when and where to use each method. Both ways are easy, and you'll be using them within no time at all!

So, let's begin by looking at how to link JavaScript to the Web page.

Get JavaScript™ 1.5 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.