January 6, 2000 - Cross-Browser Event Handler | WebReference

January 6, 2000 - Cross-Browser Event Handler

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

Yehuda Shiran, Ph.D.
Doc JavaScript

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.