Chapter 1. Adding JavaScript to a Web Page

When JavaScript was first introduced to web pages, Netscape needed to find some mechanism that allowed JavaScript to be added to a web page without causing adverse effects in other browsers. After much debate, it was finally settled on to incorporate the <script/> element, which was later added to the HTML specification.

THE <SCRIPT/> ELEMENT

The primary means of adding JavaScript to a web page is by using the <script/> element. This element, introduced by Netscape, became part of the HTML 3.2 specification as a placeholder for a transition to HTML 4.01, in which the element was fleshed out and given the following attributes:

  • src: Optional. Specifies an external script file to be executed.

  • type: Required. Specifies the language used in the <script/> element's contents, and a value of "text/javascript" is typically used. Even though this attribute is required by the HTML specification, all browsers assume the language is JavaScript if this attribute is omitted.

  • language: Deprecated. This attribute specifies the language used in the <script/> element's contents. This attribute is no longer used, as type has replaced it.

  • defer: Optional. A Boolean (true or false) value that tells the browser whether to wait to execute any code within the <script/> element until after the browser loads the entire HTML document.

  • charset: Optional. The character encoding of the external code file specified by the src attribute. This attribute is rarely used.

There are ...

Get JavaScript® 24-Hour Trainer 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.