All the executable content elements we’ve discussed so far have had one common trait: they are separate from the browser and the HTML/XHTML document—separate data, separate execution engine.

JavaScript is different. It is a scripting language that taps the native functionality of the browser. You may sprinkle JavaScript statements throughout your documents, either as blocks of code or as single statements attached to individual tags. JavaScript-enabled browsers, including all the currently popular ones, interpret and act upon the JavaScript statements you provide to do such things as alter the appearance of the document, control the display, validate and manipulate form elements, and perform general computational tasks.

As with Java, we do not pretend to teach JavaScript programming in this book. We’ll show you how to embed and execute JavaScript within your documents, but we ask that you turn to books like the companion JavaScript: The Definitive Guide (O’Reilly) for a complete reference.

The <script> Tag

One way to place JavaScript code in your document is via the HTML and XHTML standard <script> tag.

The browser processes everything between <script> and </script> as executable JavaScript statements and data. You cannot place HTML or XHTML within this tag; the browser flags it as an error.

However, browsers that do not support <script> process its contents as regular HTML, to the confusion of the user. For this reason, we recommend that you include the contents of the <script> ...

Get HTML & XHTML: The Definitive Guide, 6th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.