13.02.2013 Views

WEB STANDARDS CREATIVITY

WEB STANDARDS CREATIVITY

WEB STANDARDS CREATIVITY

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

The addEvent function primes the web page. It tells the browser what it needs to run (which JavaScript function) and when<br />

(on what event—for example, when the page loads or when something gets clicked). Here it is:<br />

function addEvent(elm, evType, fn, useCapture)<br />

{<br />

if(elm.addEventListener)<br />

{<br />

elm.addEventListener(evType, fn, useCapture);<br />

return true;<br />

}<br />

else if (elm.attachEvent)<br />

{<br />

var r = elm.attachEvent('on' + evType, fn);<br />

return r;<br />

}<br />

else<br />

{<br />

elm['on' + evType] = fn;<br />

}<br />

}<br />

Now if that looks like gobbledygook to you, don’t worry. You don’t need to understand everything it does or how. You simply<br />

need to know that it has been created to work around some cross-browser JavaScript issues, and it allows you to call it<br />

using three parameters:<br />

What element you are dealing with (elm)<br />

What the event is (click, load, focus) (evType)<br />

What JavaScript function you want it to do when that element has that event triggered (fn)<br />

And here’s an example of how you would call it:<br />

addEvent(window, 'load', addPrintLinks, false);<br />

Or, in plain English, when the window (or, to be precise, the document in this window) has loaded, please run the<br />

addPrintLinks function. This function has yet to be built, so just to test that it works, I’ve placed a simple alert there for<br />

the time being:<br />

<br />

function addPrintLinks()<br />

{<br />

alert("Here's where the cool stuff will happen");<br />

}<br />

addEvent(window, 'load', addPrintLinks, false);<br />

<br />

The alert looks like Figure 8-3.<br />

chapter 8 Print Magic: Using the DOM and CSS to Save the Planet<br />

189

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

Saved successfully!

Ooh no, something went wrong!