The Web Design Group

SCRIPT - Client-side Script

Syntax <SCRIPT>...</SCRIPT>
Attribute Specifications
  • TYPE=ContentType (content-type of scripting language)
  • LANGUAGE=CDATA (scripting language name)
  • SRC=URI (external script location)
  • CHARSET=Charset (character encoding of external script)
  • DEFER (script execution may wait)
Contents An embedded script
Contained in HEAD, block-level elements, inline elements except SELECT and SCRIPT

The SCRIPT element includes a client-side script in the document. Client-side scripts allow greater interactivity in a document by responding to user events. For example, a script could be used to check the user's form input prior to submission to provide immediate notice of any errors by the user.

Note that not all browsers support client-side scripting, and supporting browsers allow the user to disable scripting. In particular, browsers on mobile phones often ship with scripting disabled for performance reasons. Authors should avoid dependence on client-side scripting wherever possible. The NOSCRIPT element can be used to provide content for browsers that do not support client-side scripting or have it disabled. In the case of form validation, any error checking done by the client-side script should be repeated by the server-side form processor.

Also note that different browsers support different variants of scripting languages with different bugs. Authors are encouraged to check their scripts on as many browsers as possible.

The required TYPE attribute of SCRIPT specifies the media type of the scripting language, e.g., text/javascript. However, many browsers only support the deprecated LANGUAGE attribute, which specifies the language name. Examples of supported LANGUAGE values include JavaScript, JavaScript1.1, and VBScript. The values are not case sensitive.

Browsers will ignore scripts with LANGUAGE values that they do not support. For example, Netscape 3.0 will execute scripts with LANGUAGE="JavaScript" or LANGUAGE="JavaScript1.1" but will ignore scripts with LANGUAGE="JavaScript1.2" or LANGUAGE="VBScript".

In the absence of the LANGUAGE attribute, browsers that do not support the TYPE attribute typically assume that the language is the highest version of JavaScript supported by the browser. Thus authors may safely omit the deprecated LANGUAGE attribute when using JavaScript.

An embedded script is given as the content of the SCRIPT element. The SRC attribute allows authors to reuse code by specifying an external script. The optional CHARSET attribute gives the character encoding of the external script, although this attribute is rarely needed in practice. When the SRC attribute is used, the embedded script is ignored. An example follows:

<SCRIPT TYPE="text/javascript" SRC="foo.js">
  // embedded script ignored
// -->

Netscape 4.x requires that external scripts be served with a Content-Type of application/x-javascript.

The DEFER attribute indicates that the browser may wait to parse the script until the rest of the document has been rendered. Scripts that use DEFER must not generate any document content, and should not be required to respond to user events (e.g., form submission) that may occur while the document is loading. The DEFER attribute can be useful for delaying scripts that pre-load images or harass the user with scrolling messages in the status bar.

The SCRIPT element may occur any number of times in the document HEAD or BODY. Typically the SCRIPT element is used in the HEAD unless it generates BODY content.

Pre-HTML 3.2 browsers, unaware of the SCRIPT element, will treat the content of SCRIPT as normal HTML. To make these browsers ignore the SCRIPT's content, scripting languages generally allow SGML comments to be used around an embedded script. For example:

<SCRIPT TYPE="text/javascript">
<!-- comment to end of line
// comment to end of line -->

Note that "-->" is contained within a JavaScript single-line comment (started with two slashes).

Due to an unintuitive quirk of SGML, end tags are recognized within SCRIPT elements, but other kinds of markup--such as start tags and comments--are not. Almost all browsers ignore this rule, but authors should nonetheless avoid using strings such as "</P>" in their embedded scripts. JavaScript allows authors to use a backslash in the string to prevent it from being treated as an end tag, e.g., document.write("<\/P>").

More Information