spacer

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

home / experts / javascript / column9


The Event Object

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

In JavaScript 1.2 (Navigator 4.0x and above) every event (that's right, "event," not "event handler") is also an object whose properties define the characteristics of that event. That is, each event is associated with an event object, which provides information about the event, such as the type of event, and the exact position of the mouse pointer when it occurred.

Each event's event object exists immediately after the event occurs. It is automatically passed to the event processing function as an argument, provided that you take the property assignment approach. In other words, if you assign a function to an object's event handler by way of a simple assignment statement, the event object is automatically handed to the function as a single argument. On the other hand, if you specify the event handler as an attribute of an HTML tag, you must explicity hand the object to the function. Use its standard event (lowercase "e") reference to do that. Take a look at the following example:

<A HREF="http://www.netscape.com/"
   onClick="alert('This link got an event: ' + event.type)">
Netscape</A>

This link generates an alert dialog box, which displays the type of event that triggered it (click). Notice the event reference in the event handler script.

You've probably noticed the use of the e parameter in our event processing functions in this column. Although you can choose a different name, this is a common practice among scripters. In fact, you don't even need to specify a parameter if the function doesn't utilize the event's event object. The following code segments are equivalent:

<!-- first segment -->
<A HREF="http://www.netscape.com/"
   onClick="alert('This link got an event: ' + event.type)">
Netscape</A>

<!-- second segment -->
<SCRIPT LANGUAGE="JavaScript">
<!--

function displayLinkEvent(e) {
  alert("This link got an event: " + e.type);
}

// -->
</SCRIPT>

<A HREF="http://www.netscape.com/"
   onClick="displayLinkEvent(event)">
Netscape</A>

<!-- second segment -->
<A HREF="http://www.netscape.com/">Netscape</A>

<SCRIPT LANGUAGE="JavaScript">
<!--

function displayLinkEvent(e) {
  alert("This link got an event: " + e.type);
}

document.links[0].onclick = displayLinkEvent;

// -->
</SCRIPT>

Notice that the event handler attribute (second segment) must explicitly pass the Event object by way of the event keyword as an argument. The event keyword is similar to the this keyword in many ways. For example, you can pass one of its properties to the function, instead of sending the entire object:

<SCRIPT LANGUAGE="JavaScript">
<!--

function displayLinkEvent(eventType) {
  alert("This link got an event: " + eventType);
}

// -->
</SCRIPT>

<A HREF="http://www.netscape.com/"
   onClick="displayLinkEvent(event.type)">
Netscape</A>

This kind of property pre-extraction is not possible with the property assignment approach, because the entire event object is passed to the function as an argument. Another advantage of the attribute approach is that you can hand several arguments to a function when an event occurs. Here's an example:

<SCRIPT LANGUAGE="JavaScript">
<!--

function processLink(eventType, eventModifiers) {
  ...
}

// -->
</SCRIPT>

<A HREF="http://www.netscape.com/"
   onClick="processLink(event.type, event.modifiers)">
Netscape</A>

This is obviously not possible with the property assignment approach because it invokes the event processing function with one argument -- the event object.

Don't worry about the browser compatibility issues. We'll nail them down in our next column.

http://www.internet.com


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger


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

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