The SCRIPT element is the method used by browsers to recognize
scripting languages in an HTML document. Scripting (both embedded and external to
the page) allows web pages to perform actions and change dynamically in response
to events such as screen exit and entry, or user mouse-clicks.
While discussing the full scope of scripting in HTML pages is not the intent
of these documents, discussion of how scripting affects HTML authoring is
DEFINITELY relevant. Many other sites have covered the details with
far greater skill and detail than I could ever manage. Please see the
Related Links section for pointers to good
resources on the subject.
JavaScript and Other Scripting Languages
The most popular browser scripting language at this time is the
JavaScript language from Netscape. The
JavaScript language has been standardized by
ECMA as
ECMA-262,
(AKA EcmaScript.) Microsoft has also implemented a version of the JavaScript
language under the name JScript.
While JavaScript is the most widely implemented scripting language, it is possible to
use any language, provided the browser you are using can understand it. Microsoft,
for example, also makes a scripting language for the Internet Explorer browser
called VBScript (based on the Visual Basic programming language) and a few other
languages (such as Perl) are also usable with some web browsers.
Event Handlers and Script Invocation
Scripting code can be invoked either as a function call to a routine
contained in the SCRIPT element or the scripting language statements can
be included in-line within the attribute value of the attribute that
invokes it.
To allow scripting to interact dynamically with web page elements, a new class of
HTML attribute has been added called "Event
Handlers." These HTML attributes (there are now over 50 different
events) trigger script code when the action/event that they are bound to occurs
for an element.
Javascript allows an extra type of event trapping beyond event handlers. The
A HREF element can also be used to invoke
script code. The script is run when the user activates the hyperlink (see the
A HREF page for more details.)
Description:
This indicates the character encoding of the script contents.
Values:
A recognized RFC 2045 language character set string.
Default is ISO-8859-1.
Defer
2 | 3
| 3.2 | 4
| IE4 | M
| N | O
Required? No
Description:
This stand alone attribute is used to advise the browser that the
script is not going to generate any rendered document content and
thus, the user agent can continue parsing and rendering.
Values: NA
Event
2 | 3
| 3.2 | 4
| IE4 | M
| N | O
Required? No
Description:
Specifies the event the script is being written for.
Values: Event name
For
2 | 3
| 3.2 | 4
| IE4 | M
| N | O
Required? No
Description:
Specifies which element is being bound to the event script.
Values: A scripting object or element ID.
Language
2 | 3
| 3.2 | 4
| IE3B1 | M
| N2B3 | O3
Required? No
Description:
This attribute indicates the scripting language the script is written
in. It is required if the SRC attribute is not specified, optional
otherwise.
Values:
The two most popular scripting languages currently are
JavaScript and VBScript.
Src
2 | 3
| 3.2 | 4
| IE3.01 | M
| N3B5 | O3
Required? No
Description:
This attribute specifies an external source for the script code.
Values: An absolute or relative URL
Type
2
| 3 | 3.2
| 4 | IE4
| M | N??
| O
Required? No
Description:
This attribute specifies the MIME type of the scripting code.
Values: Alphanumeric MIME type
Example
<html>
<head>
<scriptLANGUAGE="JavaScript">
<!-- hide script from old browsers
function getname(str) {
alert("Hi, "+ str+"!");
}
// end hiding contents -->
</script>
</head>
<body>
Please enter your name:
<form>
<inputTYPE="text"
NAME="name"
onBlur="getname(this.value)"
VALUE="">
</form>
</body>
</html>
NOTE: Because this element has content between the start
and end tags, older browsers may end up displaying the style content it
contains. In order to prevent this, it is STRONGLY recommended to
embed this information within the SGML/HTML Comment structure
(<!-- -->).
HTML is evaluated as it is loaded. If an event handler attempts to trigger a
script that has not yet been loaded, the attempt will fail. Because of this,
it is generally safest to place a SCRIPT statement at the top of a document
in the HEAD element.
DTD Note: This element exists in the HTML 3.2
recommendation but only as a place holder until version 4.0 of HTML
which details the usage of the element.
DTD Note: The Internet Explorer 3.0
DTD lists extra supported attributes for script: NAME and TYPE.
I have not yet verified if these are really supported yet in IE3.
Browser Peculiarities
The onSelect event handler has appeared in Netscape Javascript references
since the language was first created, listing support for input type=text,
and textarea. But every test I try, even with Netscape version 4.0 seems not
to work. The support grids for this attribute reflect this. If I am wrong
and someone has gotten this to work in Netscape, please let me know what I
am doing wrong?