15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

154<br />

<strong>JavaScript</strong> <strong>Examples</strong> <strong>Bible</strong>: The Essential Companion to <strong>JavaScript</strong> <strong>Bible</strong><br />

captureEvents(eventTypeList)<br />

Compatibility ✓<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Example<br />

The page in Listing 16-21 is an exercise in capturing and releasing click events in<br />

the window object. Whenever the window is capturing click events, the flash()<br />

function runs. In that function, the event is examined so that only if the Control key<br />

is also being held down and the name of the button starts with “button” does the<br />

document background color flash red. For all click events (that is, those directed at<br />

objects on the page capable of their own onClick event handlers), the click is processed<br />

with the routeEvent() method to make sure the target buttons execute<br />

their own onClick event handlers.<br />

Listing 16-21: Capturing Click Events in the Window<br />

<br />

<br />

Window Event Capture<br />

<br />

// function to run when window captures a click event<br />

function flash(e) {<br />

if (e.modifiers = Event.CONTROL_MASK &&<br />

e.target.name.indexOf(“button”) == 0) {<br />

document.bgColor = “red”<br />

setTimeout(“document.bgColor = ‘white’”, 500)<br />

}<br />

// let event continue to target<br />

routeEvent(e)<br />

}<br />

// default setting to capture click events<br />

window.captureEvents(Event.CLICK)<br />

// assign flash() function to click events captured by window<br />

window.onclick = flash<br />

<br />

<br />

<br />

<br />

Turn window click event capture on or off (Default is “On”)<br />

&nbsp;<br />

<br />

<br />

Ctrl+Click on a button to see if clicks are being captured by the window<br />

(background color will flash red):<br />

<br />

windowObject.captureEvents()

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

Saved successfully!

Ooh no, something went wrong!