ÄúµÄλÖãºÑ°ÃÎÍøÊ×Ò³£¾±à³ÌÀÖÔ°£¾JavaScript£¾JavaScript Notes

Event handlers

The following event handlers are available in JavaScript:

  • onBlur
  • onChange
  • onClick
  • onFocus
  • onLoad
  • onMouseOver
  • onSelect
  • onSubmit
  • onUnload

    onBlur event handler

    A blur event occurs when a select, text, or textarea field on a form loses focus. The onBlur event handler executes JavaScript code when a blur event occurs.

    See the relevant objects for the onBlur syntax.

    Event Handler of

    select, text, textarea

    Examples

    In the following example, userName is a required text field. When a user attempts to leave the field, the onBlur event handler calls the required() function to confirm that userName has a legal value.

    <INPUT TYPE="text" VALUE="" NAME="userName" onBlur="required(this.value)">

    See also

  • onChange , onFocus event handlers

    onChange event handler

    A change event occurs when a select, text, or textarea field loses focus and its value has been modified. The onChange event handler executes JavaScript code when a change event occurs.

    Use the onChange event handler to validate data after it is modified by a user.

    See the relevant objects for the onChange syntax.

    Event Handler of

    select, text, textarea

    Examples

    In the following example, userName is a text field. When a user attempts to leave the field, the onBlur event handler calls the checkValue() function to confirm that userName has a legal value.

    <INPUT TYPE="text" VALUE="" NAME="userName" onBlur="checkValue(this.value)">

    See also

  • onBlur , onFocus event handlers

    onClick event handler

    A click event occurs when an object on a form is clicked. The onClick event handler executes JavaScript code when a click event occurs.

    See the relevant objects for the onClick syntax.

    Event Handler of

    button, checkbox, radio, link, reset, submit

    Examples

    For example, suppose you have created a JavaScript function called compute(). You can execute the compute() function when the user clicks a button by calling the function in the onClick event handler, as follows:

    <INPUT TYPE="button" VALUE="Calculate" onClick="compute(this.form)">

    In the above example, the keyword this refers to the current object; in this case, the Calculate button. The construct this.form refers to the form containing the button.

    For another example, suppose you have created a JavaScript function called pickRandomURL() that lets you select a URL at random. You can use the onClick event handler of a link to specify a value for the HREF attribute of the <A> tag dynamically, as shown in the following example:

    <A HREF="" onClick="this.href=pickRandomURL()" onMouseOver="window.status='Pick a random URL'; return true"> Go!</A>

    In the above example, the onMouseOver event handler specifies a custom message for the Navigator status bar when the user places the mouse pointer over the Go! anchor. As this example shows, you must return true to set the window.status property in the onMouseOver event handler.


    onFocus event handler

    A focus event occurs when a field receives input focus by tabbing with the keyboard or clicking with the mouse. Selecting within a field results in a select event, not a focus event. The onFocus event handler executes JavaScript code when a focus event occurs.

    See the relevant objects for the onFocus syntax.

    Event Handler of

    select, text, textarea

    Examples

    The following example uses an onFocus handler in the valueField textarea object to call the valueCheck() function.

    <INPUT TYPE="textarea" VALUE="" NAME="valueField" onFocus="valueCheck()">

    See also

  • onBlur , onChange event handlers

    onLoad event handler

    A load event occurs when Navigator finishes loading a window or all frames within a <FRAMESET>. The onLoad event handler executes JavaScript code when a load event occurs.

    Use the onLoad event handler within either the <BODY> or the <FRAMESET> tag, for example, <BODY onLoad="...">.

    In a <FRAMESET> and <FRAME> relationship, an onLoad event within a frame (placed in the <BODY> tag) occurs before an onLoad event within the <FRAMESET> (placed in the <FRAMESET> tag).

    Event Handler of

    window

    Examples

    In the following example, the onLoad event handler displays a greeting message after a web page is loaded.

    <BODY onLoad="window.alert("Welcome to the Brave New World home page!")>
    

    See also

    onUnload event handler


    onMouseOver event handler

    A mouseOver event occurs once each time the mouse pointer moves over an object from outside that object. The onMouseOver event handler executes JavaScript code when a mouseOver event occurs.

    You must return true within the event handler if you want to set the status or defaultStatus properties with the onMouseOver event handler.

    See the relevant objects for the onMouseOver syntax.

    Event Handler of

    link

    Examples

    By default, the HREF value of an anchor displays in the status bar at the bottom of the Navigator when a user places the mouse pointer over the anchor. In the following example, the onMouseOver event handler provides the custom message "Click this if you dare."

    <A HREF="http://home.netscape.com/" onMouseOver="window.status='Click this if you dare!'; return true"> Click me</A>

    See onClick for an example of using onMouseOver when the <A> tag's HREF attribute is set dynamically.


    onSelect event handler

    A select event occurs when a user selects some of the text within a text or textarea field. The onSelect event handler executes JavaScript code when a select event occurs.

    See the relevant objects for the onSelect syntax.

    Event Handler of

    text, textarea

    Examples

    The following example uses an onSelect handler in the valueField text object to call the selectState() function.

    <INPUT TYPE="text" VALUE="" NAME="valueField" onSelect="selectState()">

    onSubmit event handler

    A submit event occurs when a user submits a form. The onSubmit event handler executes JavaScript code when a submit event occurs.

    You can use the onSubmit event handler 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.

    See the relevant objects for the onSubmit syntax.

    Event Handler of

    form

    Examples

    In the following example, the onSubmit event handler calls the formData() function to evaluate the data being submitted. If the data is valid, the form is submitted; otherwise, the form is not submitted.

    form.onSubmit="return formData(this)"
    

    See also the examples for the form object.

    See also

  • submit object
  • submit method

    onUnload event handler

    An unload event occurs when you exit a document. The onUnload event handler executes JavaScript code when an unload event occurs.

    Use the onUnload event handler within either the <BODY> or the <FRAMESET> tag, for example, <BODY onUnload="...">.

    In a <FRAMESET> and <FRAME> relationship, an onUnload event within a frame (placed in the <BODY> tag) occurs before an onUnload event within the <FRAMESET> (placed in the <FRAMESET> tag).

    Event Handler of

    window

    Examples

    In the following example, the onUnload event handler calls the cleanUp() function to perform some shut down processing when the user exits a web page:

    <BODY onUnload="cleanUp()">
    

    See also

    onLoad event handler