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 of
scripting languages 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 class of
HTML attributes were 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 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 (HTML); defer (XHTML)
Event
[2|3|3.2|4]
[X1|X1.1]
[IE4|M|N|O]
Standards Details:
Added in HTML 4.01. Present in all HTML 4.01 DTDs. Dropped in XHTML DTDs
Required? No
Description:
Specifies the event the script is being written for.
Standards Details:
Deprecated in HTML 4.x/XHTML 1.0. Dropped in XHTML 1.1.
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:CDATA.
[The two most popular scripting languages currently are
JavaScript and VBScript]
Description:
This is a basic XML syntax that keeps all whitespace characters intact when they are parsed. Spacing
characters are almost always an important part of script syntax and string processing, so including
this attribute in the standards for this element makes sense.
Values:preserve - maintain all
whitespace characters in rendering and data storage (including multiple
spaces, tabs, carriage returns and linefeeds.)
Example
<html>
<head>
<scriptlanguage="JavaScript"
type="text/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>
[Test]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.
DTD Note: Even though the LANGUAGE attribute exists in
HTML 4.0, its usage is deprecated in favor of the TYPE attribute.
DTD Note: The EVENT and FOR attributes were
only added to HTML beginning in HTML 4.01.
Browser Peculiarities
According to a document on the Mozilla web site, Netscape 4 (and possibly
more) also supports these attributes (of course they don't have ANY
explanation with this claim): HREF, and CODEBASE. I have never seen these
attributes mentioned elsewhere, and have not been able to verify yet if
these attributes are supported or not, nor do I know their exact usage if so.