Chapter 2. JavaScript in HTML

The introduction of JavaScript into web pages immediately ran into the Web's predominant language, HTML. As part of its original work on JavaScript, Netscape tried to figure out how to make JavaScript coexist in HTML pages without breaking those pages' rendering in other browsers. Through trial, error, and controversy, several decisions were finally made and agreed upon to bring universal scripting support to the Web. Much of the work done in these early days of the Web has survived and become formalized in the HTML specification.

The <script> Element

The primary method of inserting JavaScript into an HTML page is via the <script> element. This element was created by Netscape and first implemented in Netscape Navigator 2. It was later added to the formal HTML specification. HTML 4.01 defines the following five attributes for the <script> element:

  • charset — Optional. The character set of the code specified using the src attribute. This attribute is rarely used, because most browsers don't honor its value.

  • defer — Optional. Indicates that the execution of the script can safely be deferred until after the document's content has been completely parsed and displayed.

  • language — Deprecated. Originally indicated the scripting language being used by the code block (such as "JavaScript", "JavaScript1.2", or "vbscript"). Most browsers ignore this attribute; it should not be used.

  • src — Optional. Indicates an external file that contains code to be executed. ...

Get Professional, JavaScript® for Web Developers, Second 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.