13.02.2013 Views

WEB STANDARDS CREATIVITY

WEB STANDARDS CREATIVITY

WEB STANDARDS CREATIVITY

SHOW MORE
SHOW LESS

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

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

You’ve probably guessed that we’re going to take the third route, since that’s what resolution-dependent layout is all about.<br />

The way to do this is to use some JavaScript that detects the window size as the page is loading and applies the wider style<br />

sheet if it will fit. If JavaScript happens to be turned off, the page won’t explode. Instead, the user will get the basic style<br />

sheet, which if you’ve designed it properly, should still be perfectly usable. Just consider the alternate layout a bonus for<br />

people with larger screens and JavaScript turned on (which is a pretty hefty chunk of users).<br />

The JavaScript should be placed after the style sheet declarations in the HTML:<br />

<br />

<br />

<br />

This is important, because the JavaScript is going to run immediately and try to work on the style sheets, so if they haven’t<br />

been included yet, you’ll have problems.<br />

The first thing to do inside resolution.js is to check the size of the browser window:<br />

checkBrowserWidth();<br />

function checkBrowserWidth()<br />

{<br />

var theWidth = getBrowserWidth();<br />

if (theWidth == 0)<br />

{<br />

addLoadListener(checkBrowserWidth);<br />

return false;<br />

}<br />

if (theWidth >= 960)<br />

{<br />

setStylesheet("Wide");<br />

}<br />

else<br />

{<br />

setStylesheet("");<br />

}<br />

return true;<br />

};<br />

checkBrowserWidth() begins by getting the width of the browser using getBrowserWidth():<br />

function getBrowserWidth()<br />

{<br />

if (window.innerWidth)<br />

{<br />

return window.innerWidth;<br />

}<br />

else if (document.documentElement && document.documentElement.clientWidth != 0)<br />

{<br />

chapter 9 Creating Dynamic Interfaces Using JavaScript<br />

219

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

Saved successfully!

Ooh no, something went wrong!