The Navigator Event Model: Event Processing Functions - Doc JavaScript | WebReference

The Navigator Event Model: Event Processing Functions - Doc JavaScript


Event Processing Functions

A function that processes events can return a Boolean value. For example, a link can be conditional: followed if the function that handles its click event returns true, and not followed when the function returns false. If the event is non-cancelable, returning a Boolean value simply ends the event handling for that event. The following link would normally load Netscape's front page, but the event handler stops it from going there:

<SCRIPT LANGUAGE="JavaScript">
<!--
function microsoft(e) {
  return false; // cancel event
}
// -->
</SCRIPT>
<A HREF="https://www.netscape.com/"
   onClick="return microsoft()">Netscape</A>

In this example, the event handler must explicitly return false (by returning the value that the microsoft() function returns). However, if you're assigning a function reference to the event handler as a property of its object, the function simply needs to return the desired Boolean value. Here's an example:

<FORM NAME="myForm" ACTION="...">
.
.
.
<INPUT TYPE="submit" VALUE="submit">
</FORM>
<SCRIPT LANGUAGE="JavaScript">
<!--
function cancelSubmit(e) {
  return false; // cancel event
}
document.myForm.onsubmit = cancelSubmit;
// -->
</SCRIPT>

Remember that JavaScript must be enabled in order to cancel a user-initiated event such as a form submission or a link click. Event processing functions that return a Boolean value are often used in form validation scripts, where the function that evaluates the user's input returns false in the form's onsubmit event handler, if an invalid entry is found. This can be done by specifying the event handler as an HTML attribute or a JavaScript property. When an event processing function returns false at the window level, the event is not handled by any other object, such as a button in the document or a child frame of the window. You can use this feature, for example, to suppress the right mouse button in an application.

https://www.internet.com


Created: December 16, 1997
Revised: December 16, 1997

URL: https://www.webreference.com/js/column9/functions.html