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.

220<br />

return document.documentElement.clientWidth;<br />

}<br />

else if (document.body)<br />

{<br />

return document.body.clientWidth;<br />

}<br />

return 0;<br />

};<br />

There are actually a few ways of determining the width of the browser window, depending on which browser you’re using,<br />

and each of the conditions in getBrowserWidth() matches one of these. Firefox, Mozilla, Safari, and Opera use<br />

window.innerWidth. Internet Explorer 6 and 7 use document.documentElement.clientWidth.<br />

In a rather strange quirk that document.documentElement.clientWidth exists in Internet Explorer 5 and 5.5, but it is always<br />

set to 0, so we have to check for this and default to document.body.clientWidth if that’s the case. The only problem with<br />

this is that the body element must exist before Internet Explorer 5.x can figure out the browser window width, so we have to<br />

reschedule the call to getBrowserWidth() once the page has been loaded. This is done in the first condition inside check-<br />

BrowserWidth(). If getBrowserWidth() returns 0, it means that the browser is probably Internet Explorer 5.x, so we create<br />

a new load event listener and wait for the page to load before doing any style sheet switching.<br />

addLoadListener() is a generic page load event handler that abstracts around some of the differences in browser event<br />

handling:<br />

function addLoadListener(fn)<br />

{<br />

if (typeof window.addEventListener != 'undefined')<br />

{<br />

window.addEventListener('load', fn, false);<br />

}<br />

else if (typeof document.addEventListener != 'undefined')<br />

{<br />

document.addEventListener('load', fn, false);<br />

}<br />

else if (typeof window.attachEvent != 'undefined')<br />

{<br />

window.attachEvent('onload', fn);<br />

}<br />

else<br />

{<br />

return false;<br />

}<br />

return true;<br />

};<br />

This and another abstracted event handler are included in event_listeners.js, because you’ll probably use them regularly<br />

on other projects.

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

Saved successfully!

Ooh no, something went wrong!