11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

alert("Got a click: " + e);<br />

// IE5&6 will show an undefined in alert since they are not DOM2<br />

document.getElementById("myElement").onclick = handleClick;<br />

//-->><br />

<br />

Notice in this example how the handler accepts an argument. DOM2 browsers pass an Event<br />

object containing extra information about the event to handlers. <strong>The</strong> name of the argument is<br />

arbitrary, but ―event,‖ ―e,‖ and ―evt‖ are most commonly used. We‘ll discuss the Event object in<br />

more detail in an upcoming section.<br />

DOM2 Event Binding Methods<br />

You can also use the new addEventListener() method introduced by DOM2 to engage an<br />

event handler in a page. <strong>The</strong>re are three reasons you might wish to use this function instead of<br />

directly setting an object‘s event handler property. <strong>The</strong> first is that it enables you to bind multiple<br />

handlers to an object for the same event. When handlers are bound in this fashion, each<br />

handler is invoked when the specified event occurs, though the order in which they are invoked<br />

is arbitrary. <strong>The</strong> second reason to use addEventListener() is that it enables you to handle<br />

events during the capture phase (when an event ―trickles down‖ to its target). Event handlers<br />

bound to event handler attributes like onclick and onsubmit are only invoked during the<br />

bubbling phase. <strong>The</strong> third reason is that this method enables you to bind handlers to text<br />

nodes, an impossible task prior to DOM2.<br />

<strong>The</strong> syntax of the addEventListener() method is as follows:<br />

object.addEventListener(“event“, handler, capturePhase);<br />

object is the node to which the listener is to be bound.<br />

"event" is a string indicating the event it is to listen for.<br />

handler is the function that should be invoked when the event occurs.<br />

capturePhase is a Boolean indicating whether the handler should be invoked during the<br />

capture phase (true) or bubbling phase (false).<br />

For example, to register a function changeColor() as the capture-phase mouseover handler for<br />

a paragraph with id of myText you might write<br />

document.getElementById('myText').addEventListener("mouseover",<br />

changeColor, true);<br />

To add a bubble phase handler swapImage():<br />

document.getElementById('myText').addEventListener("mouseover",<br />

swapImage, false);<br />

Handlers are removed using removeEventListener() with the same arguments as given when<br />

the event was added. So to remove the first handler in the previous example (but keep the<br />

second) you would invoke

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!