<SCRIPT> | NN 2 IE 3 HTML 4 | |||||||||
<SCRIPT>...</SCRIPT> | HTML End Tag: Required | |||||||||
The script element provides a container for lines of script code written in any scripting language that the browser is capable of interpreting. Script statements that are not written inside a function definition are executed as the page loads; function definitions are loaded but their execution is deferred until explicitly invoked by user or system action (events). You can have more than one script element in a document, and you may include script elements written in different script languages within the same document. An important shift in attribute syntax is introduced with HTML 4. To specify the scripting language of the statements within a script element, the language attribute has been used since the first scriptable browsers. HTML 4 deprecates that attribute in favor of the type attribute, whose value is a MIME type. Until you know for certain that your page visitors use only newer browsers that support the type attribute, you should include both attributes in documents for long-term backward compatibility with older browsers. The language attribute validates with transitional DTDs. All but the earliest scriptable browsers also allow script statements to be imported into the document from a document whose URL is specified for the src attribute. Older, nonscriptable browsers don't recognize the script element and may attempt to render the script statements as regular HTML content. To prevent this, wrap the script statements inside HTML block comment markers. The end-of-comment marker (-->) must be preceded by a JavaScript comment marker (//) to prevent JavaScript from generating a script error. Due to character conflicts between JavaScript and XHTML, and the lack of browser support for the XHTML-preferred <![CDATA[...]]> script wrapper, you should use imported script libraries for pages that must validate under XHTML. This prevents XML parsers from misinterpreting symbols such as the less-than operator (<) as XML markup symbols. |
||||||||||
Example | ||||||||||
<SCRIPT type="text/javascript" language="JavaScript"> <!-- FUNCTION howdy( ) { alert("Hello, HTML world!"); } //--> </SCRIPT> <SCRIPT type="text/javascript" scr="scripts/myscript.js"></SCRIPT> |
||||||||||
Element-Specific Attributes | ||||||||||
|
||||||||||
Element-Specific Event Handler Attributes | ||||||||||
None. |
charset | NN 6 IE n/a HTML 4 |
charset="characterSet" | Optional |
Character encoding of the content in the file referred to by the src attribute. |
|
Example | |
<SCRIPT charset="csISO5427Cyrillic" src="moscow.js"> . . . </SCRIPT> |
|
Value | |
Case-insensitive alias from the character set registry (ftp://ftp.isi.edu/in-notes/iana/assignments/character-sets). |
|
Default | |
Determined by browser. |
defer | NN 6 IE 4 HTML 4 |
defer | Optional |
The presence of the defer attribute instructs the browser to render regular HTML content without looking for the script to generate content as the page loads. This is an advisory attribute only. The browser doesn't have to hold up rendering further HTML content as it parses the content of the script element in search of document.write( ) statements. As of Version 7, Netscape allows but does not respond to the defer attribute. |
|
Example | |
<SCRIPT type="text/javascript" language="JavaScript" defer>...</SCRIPT> |
|
Value | |
The presence of this attribute sets its value to true. |
|
Default | |
false |
|
Object Model Reference | |
[window.]document.getElementById(elementID).defer
|
event | NN n/a IE 4 HTML |4| |
event="eventName" | Optional |
Internet Explorer's event model allows the binding of object events to script elements with the help of the event and for attributes. As the page loads, the browser registers each script element with its event and object binding so that when the object generates the event, the script statements inside the script element executewithout having to write event handlers for the objects or wrap the script statements inside function definitions. Event values are written either as unquoted event names or as quoted event names formatted as functions (with trailing parentheses and optional parameter names). Use this type of script-event binding only in Internet Explorer. Navigator attempts to execute the script statements while the page loads. The transitional HTML 4 DTD reserves this attribute for possible future use, but the reservation doesn't hold for XHTML 1.0. |
|
Example | |
<SCRIPT for="window" event="onresize( )">...</SCRIPT> |
|
Value | |
Case-sensitive event name or the event name as a function inside a quote pair. The object described in the for attribute must support the event named in the event attribute. |
|
Default | |
None. |
|
Object Model Reference | |
[window.]document.getElementById(elementID).event
|
for | NN n/a IE 4 HTML |4| |
for="elementID" | Optional |
Internet Explorer's event model allows the binding of object events to script elements with the help of the event and for attributes. As the page loads, the browser registers each script element with its event and object binding so that when the object generates the event, the script statements inside the script element executewithout having to write event handlers for the objects or wrap the script statements inside function definitions. Use the unique id attribute value of the element whose event you wish to handle. Use this type of script-event binding only in Internet Explorer. Navigator attempts to execute the script statements while the page loads. The transitional HTML 4 DTD reserves this attribute for possible future use, but the reservation doesn't hold for XHTML 1.0. |
|
Example | |
<SCRIPT for="firstNameEntry" event="onchange( )">...</SCRIPT> |
|
Value | |
Case-sensitive ID value of the event-generating element. The object described in the for attribute must support the event named in the event attribute. |
|
Default | |
None. |
|
Object Model Reference | |
[window.]document.getElementById(elementID).htmlFor
|
language | NN 2 IE 3 HTML 4 |
language="scriptingLanguage" | Optional |
Sets the scripting language for script statements defined in the element. This attribute is deprecated in HTML 4 (in favor of the type attribute), but it has been so widely used since the first days of scriptable browsers that its use and support will continue for a long time to come. Moreover, it is so far the only accepted way to convey the JavaScript version for the script block (e.g., see the Array object in Chapter 12). |
|
Example | |
<SCRIPT language="JavaScript">...</SCRIPT> |
|
Value | |
Internet Explorer recognizes four case-insensitive language names: JavaScript | JScript | vbs | vbscript. Navigator recognizes only JavaScript. Versions of JavaScript are also supported in appropriate browsers. To keep the attribute values one-word identifiers, the version numbers are tacked onto the end of the "JavaScript" language name. The version-less "JavaScript" is observed by all browsers; "JavaScript1.1" is recognized only by Navigator 3; "JavaScript1.2" is recognized by Navigator 4.0-4.05 and Internet Explorer 4; "JavaScript1.3" applies to Navigator 4.06-4.7x and IE 5-6; "JavaScript1.5" applies to Netscape 6-7. When script elements are assigned these later version values, older browsers that don't support the named version ignore the script elements. |
|
Default | |
JavaScript (NN); JScript (IE). |
|
Object Model Reference | |
[window.]document.getElementById(elementID).language
|
src | NN 3 IE 4 HTML 4 |
src="URL" | Optional |
Imports a file of script statements from an external file. Once the external statements are loaded, the browser treats them as if they were embedded in the main HTML document. This attribute had some support in Internet Explorer 3, but it relied on a specific JScript.dll version, which makes it unreliable to blindly use in IE 3. In theory, you should be able to add script statements inside a script element that loads an external script library file. In practice, it is more reliable to provide a separate script element for each external library file and for in-document scripts. Current implementations limit the src attribute to point to JavaScript external files. Such files must have a .js filename extension, and the server must have the extension and application/x-javascript MIME type set to serve up such files. When assigning the src attribute in an XHTML document, browsers may not like the shortcut end tag format. Don't think of the tag as an empty element, but rather as one with content that arrives from an external source. Use an explicit </SCRIPT> end tag. |
|
Example | |
<SCRIPT language="JavaScript" type="text/javascript" src="stringParseLib.js"> </SCRIPT> |
|
Value | |
Any valid URL. Current browsers require files with names that end in the .js extension. A complete URL may help overcome difficulties in earlier browsers that implement this feature. |
|
Default | |
None. |
|
Object Model Reference | |
[window.]document.getElementById(elementID).src
|
type | NN 6 IE 4 HTML 4 |
type="MIMEType" | Required |
An advisory about the content type of the script statements. The content type should tell the browser which scripting engine to use to interpret the script statements. The type attribute will eventually replace the language attribute as the one defining the scripting language in which the element's statements are written. To be compatible with future and past browsers, you may include both the language and type attributes in a script element. |
|
Example | |
<SCRIPT type="text/javascript" language="JavaScript">...</SCRIPT> |
|
Value | |
Case-insensitive MIME type. Values are limited to one(s) for which a particular browser is equipped. IE 4 and later and Netscape 6 accept text/javascript and application/x-javascript for scripts in an ECMAScript-compatible language. IE also accepts the following types: text/ecmascript, text/jscript, text/vbs (IE/Windows), text/vbscript (IE/Windows), and text/xml (IE 5 and later). |
|
Default | |
None. |
|
Object Model Reference | |
[window.]document.getElementById(elementID).type
|
version | NN n/a IE n/a HTML n/a |
version="x.y" | Optional |
This attribute is listed here as a possible future implementation for Mozilla-based browsers. Most of the pieces that support this attribute (to complement the type attribute) are in place in the Netscape 6 (Mozilla) browser engine, but as of Netscape 7, are not yet connected. |
|
Example | |
<SCRIPT type="text/javascript" version="1.5">...</SCRIPT> |
|
Value | |
Language version expressed as major and minor version integers, separated by a period. |
xml:space | NN n/a IE n/a HTML X1.0 |
xml:space="preserve" | Optional |
An XHTML parser is supposed to expunge all source code whitespace as it processes the document. This removal may harm scripts. By including the XML namespace space attribute, you instruct the parser to keep source code whitespace of script element content intact. |
|
Example | |
<SCRIPT type="text/javascript" xml:space="preserve">...</SCRIPT> |
|
Value | |
Constant value: preserve. |
|
Default | |
None. |