spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column9


Event Processing Functions

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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="http://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.

http://www.internet.com

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


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

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