HTML Reference Guide


Tag Index| Content List| HTML FAQ

<SCRIPT> ... </SCRIPT> SCRIPT

Description:

The Script element is the method used by browsers to recognize scripting languages in an HTML document. Scripting allows web pages to change dynamically in response to events such as screen exit and entry, or user mouse-clicks. The most popular browser scripting languages currently are JavaScript and VBScript. 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.

Scripts can exist either embedded within a document or may be located elsewhere. If the script code is contained within a web page, it will be embedded within an HTML comment between the Script container tags (nesting within a comment is important in order to make the script invisible to browsers that do not support the feature.) Statements are evaluated when the document is loaded. If a Script attempts to reference document objects defined by HTML elements occurring later in the document, 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.

NOTE: Scripts can also be invoked using the A element combined with Script code in place of the destination URL. This allows a script (either inline or located in a SCRIPT element) to be executed when the user clicks on a hyperlink.

Attributes:

charset - This indicates the character encoding of the script contents.

defer - 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.

language - This attribute indicates the scripting language the script is written in. It is required if the SRC attribute is not specified, optional otherwise.

src - This attribute specifies an external source for the script code.

type - This attribute specifies the MIME type of the scripting code.

onblur - A blur Event Handler executes Script code when a form field or layer loses focus.

onchange - A change Event Handler executes Script code when a form field loses focus and its value has been modified. This event handler is used to validate data after it is modified by a user.

onclick - A click Event Handler executes Script code when an object is clicked.

onfocus - A focus Event Handler executes Script code when a form field or layer receives input focus by tabbing with the keyboard or clicking with the mouse. Important: Selecting within a form field results in a onSelect event, not a onFocus event.

onload - This attribute executes Script code with the completion of loading of a window, layer, or when all frames within a Frameset have finished loading. In a Frame document scenario, an onLoad event in the BODY element of a sub-frame will occur before an onLoad event within the parent FRAMESET element.

onmouseover - A mouseOver Event Handler executes Script code once each time the mouse pointer enters the bounds of an object from outside the object.

onmouseout - A mouseOout Event Handler executes Script code once each time the mouse pointer exits the bounds of an object from inside the object.

onselect - A select Event Handler executes Script code when a user selects some of the text within a form field.

onsubmit - A submit Event Handler executes Script code when a user submits a form. It can also be used to prevent a form from being submitted; to do so, put a return statement that returns false in the event handler. Any other returned value lets the form submit. If you omit the return statement, the form is submitted.

onunload - An unLoad Event Handler executes Script code when the user exits a document. In a Frame document scenario, an onUnload event in the BODY element of a sub-frame will occur before an onUnload event within the parent FRAMESET element.

Example code:

<p><html><head><script LANGUAGE="JavaScript"><!-- hide script from old browsers function getname(str) { alert("Hi, "+ str+"!"); } // end hiding contents --></script></head><body> Please enter your name:<form><input TYPE="text" NAME="name" onBlur="getname(this.value)" VALUE=""></form></body></html><p>

Result:

See Also: