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.

Most browsers won’t have to go through the page load rigmarole, however. checkBrowserWidth() will receive the browser<br />

width immediately and be able to check it. This is done through a simple conditional—we check theWidth against our predetermined<br />

value. In our example, we use 960 as a threshold because that’s a nice, safe value for a maximized window size<br />

at a resolution of 1024✕768 if we subtract some space for browser chrome like scrollbars. If you want your style sheet to<br />

change at a different resolution, just change this value to whatever you need.<br />

If the window width is greater than or equal to 960 pixels, we activate the alternate style sheet by calling setStylesheet()<br />

with the appropriate style sheet title, in this case Wide:<br />

function setStylesheet(styleTitle)<br />

{<br />

var links = document.getElementsByTagName("link");<br />

for (var i = 0; i < links.length; i++)<br />

{<br />

if (links[i].getAttribute("rel") == "alternate stylesheet")<br />

{<br />

links[i].disabled = true;<br />

if(links[i].getAttribute("title") == styleTitle)<br />

{<br />

links[i].disabled = false;<br />

}<br />

}<br />

}<br />

return true;<br />

};<br />

setStylesheet() takes a style sheet title and iterates through all of the link elements in the page, checking whether their<br />

rel attribute matches alternate stylesheet. If that link is an alternate style sheet, its disabled property is set to true<br />

(turning any unselected style sheets off), and then the title attribute is checked to see whether it is Wide. If it is a match,<br />

disabled is set to false (turning on the style sheet).<br />

After that loop has finished, the selected alternate style sheet will be switched on and all others turned off. The same function<br />

is used if the browser width is not greater than our threshold. We call setStylesheet() with an empty string (""), and<br />

this turns off all alternate style sheets because none of their titles will match.<br />

Once all this has happened, the correct style sheet will be selected, and the page should now be displaying with the optimal<br />

layout for the user’s browser size.<br />

The last thing to do is to add in an event listener for when someone resizes the browser window. It’s all well and good to<br />

detect the browser size when someone first visits a page, but if he changes the window size, we’ll want to change the layout<br />

accordingly. To do this, we simply add an event listener for the window resize event that calls checkBrowserWidth() again:<br />

attachEventListener(window, "resize", checkBrowserWidth, false);<br />

Again, here I use a generic event handler (from event_listeners.js) that abstracts around some of the differences in<br />

browser event handling:<br />

function attachEventListener(target, eventType, functionRef, capture)<br />

{<br />

if (typeof target.addEventListener != "undefined")<br />

{<br />

target.addEventListener(eventType, functionRef, capture);<br />

chapter 9 Creating Dynamic Interfaces Using JavaScript<br />

221

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

Saved successfully!

Ooh no, something went wrong!