spacer

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

home / programming / javascript / events current pageTo page 2
[next]

Current Events: A Client Side Tipsheet

Developer News
SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear

By Kenneth Tibbetts (yankee@yankeeweb.com)

There are nearly always events on Web pages--even the simplest pages--and these events always have to be handled, somehow, by the client software. Hyperlinks and anchors have been around since the beginnings of the Web. Forms, with their input text boxes, options lists and buttons are still a common way to facilitate a user's interaction with a site.

Hyperlinks and forms do most of their stuff on the server, but it is important to not lose sight of the fact that nothing happens before the browser deals with the user input--mousing over a link lights up the link, say, and writes the URL to the status bar. Clicking submit calls a function on the browser before sending anything to be processed. Even on clients that don't do scripting you have a fairly hefty amount of event handling going on.

There may be a basic functionality you want to give your pages for users with older browsers, and for these clients, links and forms are it. By keeping your HTML simple, and adding functionality through scripts, you ensure that every visitor will get the most out of the page.

And even in the most modern browser, bulging with muscular methods and more properties than Masterpiece Theater, using links and forms is often the best way to get users to interact with the page. They are familiar elements; the visitor knows immediately what to do.

A nice thing about the new browsers is they support events on just about any HTML element you can think of. Event handling, however, is not much different than it was yesterday. It is still a browser specific, not quite standardized porridge of incompatable methods and properties. I use the DOM recommendations as much as I can, but in the area of event handling, we are still pretty much on our own. I'll give you a few examples.

Most of the time today, the best way to set up an event handler is the simplest, and the oldest--set a property of an HTML element. This code will work in most browsers:

document.links[0].onmouseover=writeStatus;

// where writeStatus is a function that 
// handles a mouseover event:

function writeStatus(e){
   var thisStatus=Eve(e).href.pathName;
   var ax=thisStatus.lastIndexOf('\/')+1;
   if(ax!=-1) thisStatus=thisStatus.substring(ax);
   window.status= thisStatus;
   return true;
}

Note that you don't define the argument (e).

The argument is the sniffer--Netscape Navigator and N6/ Mozilla automatically pass an argument for each event. This is basically the DOM scheme as well. IE doesn't pass an argument, but every event instantiates a global window.event object.

You could handle your events in the first lines of every function you write; but save yourself some typing. Putting a few generic event handlers in your code makes it easy to keep up with the vendors--change once, read many.

Call on an event examiner for the details you need about the event. Most often I want to know what element has triggered the event, which is Eve's specialty.

Who is it?

function Eve(e){
   var trigger='';
   if(!e && event.srcElement) trigger= event.srcElement;
   else if(e.target) trigger= e.target;
   if (trigger.nodeType && trigger.nodeType== 3)
      trigger= trigger.parentNode;
   return trigger;
}

If there is an argument (e) you know you have an event argument to work with. Otherwise see if you are working with IE. The DOM is gonna use target and currentTarget (more on this on page 2), Netscape 4 and Opera use target, and IE has the lovely srcElement.

Whichever, the browser examines the event and returns a reference to the element that triggered it.

If you need to use the DOM event phase, which captures events on the way up or down through the document, precede the e.target branch with:

else if(e.currentTarget) trigger=e.currentTarget

I rarely use it, but probably will when IE gets on board.

I have used this little function for years. It works in everything from Netscape's version 3 on. I have added a line for DOM aware browsers, to bump up a text node to its parent, because most of the time I work on the element level. But I like to keep it as simple as possible, and as such it gets called a lot.


home / programming / javascript / events current pageTo page 2
[next]



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
How to Create an Ajax Autocomplete Text Field: Part 6 · Software Engineering for Ajax · Perl Pragma Primer
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Using File Virtualization for Disaster Recovery · VoIP Security: SIP—Versatile but Vulnerable · Around the World in 80 Nodes

Created: April 10, 2002
Revised: April 10, 2002

URL: http://webreference.com/programming/javascript/events/