JavaScript

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 single statements attached to individual tags. The JavaScript-enabled browsers, including both Netscape and Internet Explorer, 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 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.

Everything between <script> and </script> is processed by the browser as executable JavaScript statements and data. You cannot place HTML or XHTML within this tag; it is flagged as an error by the browser.

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 ...

Get HTML & XHTML: The Definitive Guide, 5th Edition 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.