Getting Event Information

You might have noticed that I declared the function displayMessage as displayMessage(e) in the previous example. Why did I indicate that this function would be passed an argument? The answer is that it is passed an argument in the Netscape Navigator. This argument is an object of the event class, and you can get information about the event (such as where a mouse click occurred) using this object, which I've named e . You'll find the using this object, which I've named e . You'll find the properties of the event class in Netscape Navigator in Table 6-7.

However, Internet Explorer handles things differently, of course. In Internet Explorer, event-handling functions are not passed an event object. (JavaScript is flexible enough that even if you declare a function as we have here to handle both browsers, as though it does receive an argumentalthough it really doesn'tthere's no problem.) Instead of passing an event object to event-handling functions, you use the window.event object (that is, the event subobject of the window object), which is available globally in code and doesn't have to be passed to a function. You'll find the properties of the window.event property in Internet Explorer in Table 6-8.

Table 6-7. Netscape Navigator's Event Object Properties
Property Means
data Holds an array of strings containing the URLs of the dropped objects. It is used with the dragdrop event.
height Holds a height associated with the event.
layerX Holds the cursor's horizontal position in pixels, relative to the layer in which the event occurred.
layerY Holds the cursor's vertical position in pixels, relative to the layer in which the event occurred.
modifiers Holds modifier keys associated with a mouse or key event. Possible values are ALT_MASK , CONTROL_MASK , SHIFT_MASK , and META_MASK .
pageX Holds the cursor's horizontal position in pixels, relative to the page.
pageY Holds the cursor's vertical position in pixels, relative to the page.
screenX Holds the cursor's horizontal position in pixels, relative to the screen.
screenY Holds a vertical position in pixels, relative to the screen.
type Holds the type of event.
which Indicates the mouse button that was pressed or the ASCII value of a pressed key.
width Holds a width associated with the event.
Table 6-8. Internet Explorer's window. event Object Properties
Property Means
altKey Is true if the Alt key was down
altLeft Is true if the left Alt key was down
button Specifies which mouse button, if any, was pressed
cancelBubble Indicates whether this event should move up the event hierarchy
clientX Holds an x coordinate with respect to the client area
clientY Holds a y coordinate with respect to the client area
ctrlKey Is true if the Ctrl key was down
ctrlLeft Is true if the left Ctrl key was down
fromElement Specifies an element being moved
keyCode Holds the code of the struck key
offsetX Holds a container-relative x position
offsetY Holds a container-relative y position
reason Holds information about a data transfer
returnValue Specifies the return value of the event
screenX Holds an x coordinate relative to physical screen size
screenY Holds a y coordinate relative to physical screen size
shiftKey Is true if the Shift key was down
shiftLeft Is true if the left Shift key was down
srcElement Holds the element that caused the event
srcFilter Holds a filter event if this is a filterChange event
toElement Specifies the element being moved to
type Property event type as a string
x Holds an x position of the event in context
y Holds a y position of the event in context

For an example that uses the event objects in Tables 6-7 and 6-8, see the next section.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net