HTML XHTML The Complete Reference
home » reference » appendix a » html element reference

<script> (Scripting)

This element encloses statements in a scripting language for client-side processing. Scripting statements can either be included inline or loaded from an external file and might be commented out to avoid execution by nonscripting-aware browsers.

Standard Syntax

charset="character set"
id="unique alphanumeric identifier"
language="scripting language name"
src="url of script code"
type="mime type"

Attributes Defined by Internet Explorer

event="event name" (3)
for="element ID" (3)

Events Defined by Internet Explorer

onload, onpropertychange, onreadystatechange

Element Specific Attributes

This attribute defines the character encoding of the script. The value is a space- and/or comma-delimited list of character sets as defined in RFC 2045. The default value is ISO-8859-1.

Presence of this attribute indicates that the browser might defer execution of the script enclosed by the <script> element. In practice, deferring code might be more dependent on the position of the <script> element or the contents. This attribute was added very late to the HTML 4.01 specification and its support is currently minimal.

This Microsoft attribute is used to define a particular event that the script should react to. It must be used in conjunction with the for attribute. Event names are the same as event handler attributes; for example, onclick, ondblclick, and so on.

The for attribute is used to define the name or ID of the element to which an event defined by the event attribute is related. For example, <script event="onclick" for="button1" language="vbscript"> defines a VBScript that will execute when a click event is issued for an element named button1.

This attribute specifies the scripting language being used. The Netscape implementation supports JavaScript. The Microsoft implementation supports JScript (a JavaScript clone) as well as VBScript, which can be indicated by either vbs or vbscript. Other values that include the version of the language used, such as JavaScript1.1 and JavaScript1.2, also might be possible and are useful to exclude browsers from executing script code that is not supported.

This attribute specifies the URL of a file containing scripting code. Typically, files containing JavaScript code will have a .js extension, and a server will attach the appropriate MIME type; if not, the type attribute might be used to explicitly set the content type of the external script file. The language attribute also might be helpful in determining this.

This attribute should be set to the MIME type corresponding to the scripting language used. For JavaScript, for example, this would be text/javascript. In practice, the language attribute is the more common way to indicate which scripting language is in effect.

This attribute is included from XHTML 1.0 on, and is used to set whether spaces need to be preserved within the script element or whether the default white space handling should be employed. In practice, this attribute is not used by developers.


<script language="JavaScript">
<!-- alert("Hello World !!!"); // -->

<!-- code in external file -->
<script language="JavaScript1.2" src="superrollover.js">


HTML 4 and 4.01, XHTML 1.0, 1.1
Internet Explorer 3, 4, 5, 5.5, 6
Netscape 2, 3, 4, 4.5-4.8, 6, 7
Opera 4-7


It is common practice to comment out statements enclosed by the <script> element. Without commenting, scripts are displayed as page content by browsers that do not support scripting. The particular comment style might be dependent on the language being used. For example, in JavaScript use

<script language="JavaScript">
<!-- Javacript code here // -->
and in VBScript use
<script language="vbscript">
<!-- VBScript code here -->
The HTML 3.2 specification defined a placeholder <script> element.

The event and for attributes are defined under transitional versions of HTML 4.01 but only as reserved values. Later specifications appear to have dropped potential support for them, though they continue to be supported by Internet Explorer.

Most browsers assume JavaScript when parsing a script element without a set type or language attribute.

Refer to the <noscript> tag reference in this appendix to see how content might be identified for nonscripting-aware browsers.

(X)HTML Elements
CSS Properties
Previous: samp Next: select
< Home | About | Chapters | Examples | Errata | Reference | Site Map >