O'Reilly logo

Professional, JavaScript® for Web Developers, Second Edition by Nicholas C. Zakas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

  • type — Required. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required