spacer
Yehuda Shiran January 6, 2000
Cross-Browser Event Handler
Tips: January 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Google Chrome Playing Catch-Up on Extensions
Open Solutions Alliance Gets New Leadership
Red Hat Spacewalk Expands Linux Management
With Internet Explorer 4.0x and up, every HTML element on the page can be the source for a full set of mouse and keyboard events. For example, the following elements support the onmouseup event handler:

A, ADDRESS, APPLET, AREA, B, BIG, BLOCKQUOTE, BODY, BUTTON, CAPTION, CENTER, CITE, CODE, DD, DFN, DIR, DIV, DL, DT, EM, EMBED, FIELDSET, FONT, FORM, H1, H2, H3, H4, H5, H6, HR, I, IMG, INPUT, KBD, LABEL, LEGEND, LI, LISTING, MAP, MARQUEE, MENU, OBJECT, OL, OPTION, P, PLAINTEXT, PRE, S, SAMP, SELECT, SMALL, SPAN, STRIKE, STRONG, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TFOOT, TH, THEAD, TR, TT, U, UL, VAR, XMP, document

Although Navigator 4.0x greatly expanded its set of elements that generate events, the selection is still much more limited. Not all elements can be the source of a mouse or keyboard event. For comparison, here's a list of the elements that support the onmouseup event handler in Navigator 4.0x:

Needless to say, there are only several elements that accept the onmouseup event handler in Navigator 4.0x and up. There is obviously no way to specify an event handler for an element that doesn't support it. Since Internet Explorer 4.0x and up normally features much more elements that support a given event handler, you should only use those that are also supported by Navigator 4.0x and up when writing a cross-browser script. In any case, if you add an event handler to an element that doesn't support that type of event handler, it has no effect. Regardless of whether you specify the event handler as a property or as an HTML attribute, the browser does not generate an error (because HTML never causes explicit errors, and adding a new property to an existing object is a legal JavaScript operation). For example, Navigator 4.0x and up ignores the following event handler specification, whereas Internet Explorer 4.0x and up supports it:

<P onClick="alert('This only works with Internet Explorer 4.0x.')"></P>

The P element in Navigator 4.0x does not support any event handlers, so this definition does not work. In fact, you can find out for yourself by clicking these words, because we have specified an onclick event handler for this paragraph. The alert dialog box should pop up if you are running Internet Explorer 4.0x and up. Otherwise nothing occurs when you click the paragraph.

Learn more about the Cross-Browser Event Model from Column 11, The Cross-Browser Event Model.


People who read this tip also read these tips:

Look for similar tips by subject:

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

webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MS Access and MySQL · Cisco AutoQoS: VoIP QoS for Mere Mortals · While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags