14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5 ■ presentatiOn anD BehaviOr (Css anD event hanDling)<br />

The function uses one more parameter than addEventListener(), which is the element itself. It tests whether<br />

addEventListener() is supported <strong>and</strong> simply returns true when it is able to attach the event in the W3C-compliant way.<br />

Otherwise, it checks whether attachEvent() is supported (effectively meaning IE 8 or below is used) <strong>and</strong> tries to<br />

attach the event that way. Notice that attachEvent() does need the on prefix for the event. For browsers that support<br />

neither addEventListener() nor attachEvent(), like an extremely old browser, the function points the <strong>DOM</strong>-1<br />

property to the function.<br />

■ Note there is an ongoing discussion about how addEvent() can be improved—for example, to support retaining the<br />

option to send the current element as a parameter via this—<strong>and</strong> many clever solutions have been developed so far. Because<br />

each solution has different drawbacks, i won’t go into details here, but if you are interested, check the comments at the<br />

addEvent() recoding contest page at http://www.quirksmode.org/blog/archives/2005/10/_<strong>and</strong>_the_winner_1.html.<br />

Because IE uses a global event, you cannot rely on the event object being sent to your listeners. Instead, you<br />

need to write a different function to get the element that was activated. Matters get confused even further because the<br />

properties of window.event are slightly different from the ones of the W3C event object:<br />

• In Internet Explorer, target is replaced by srcElement.<br />

• button returns different values. In the W3C model, 0 is the left button, 1 is the middle, <strong>and</strong><br />

2 is the right; however, IE returns 1 for the left button, 2 for the right, <strong>and</strong> 4 for the middle. It<br />

also returns 3 when the left <strong>and</strong> right buttons are pressed together, <strong>and</strong> it returns 7 for all three<br />

buttons pressed together.<br />

To accommodate these changes, you can use this function:<br />

function getTarget(e){<br />

var target;<br />

if(window.event){<br />

target = window.event.srcElement;<br />

} else if (e){<br />

target = e.target;<br />

} else {<br />

target = null ;<br />

}<br />

return target;<br />

}<br />

Or more briefly, using the ternary operator:<br />

getTarget:function(e){<br />

var target = window.event ? window.event.srcElement :<br />

e ? e.target : null;<br />

if (!target){return false;}<br />

return target;<br />

}<br />

138<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!