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.

74<br />

Inspired by a venerable JavaScript-based style sheet switcher (www.alistapart.com/stories/alternate), the New York<br />

Magazine’s text-sizing code performs a number of different tasks. When a user clicks one of these links, an additional class<br />

is appended to the body element that, when present, will change the size of the text in the article template accordingly.<br />

Additionally, a cookie stored on the user’s browser remembers the user’s selection and automatically applies the appropriate<br />

text size upon his or her return. It’s a rather convoluted piece of JavaScript, but that might be because we’re not exactly<br />

the Samuel L. Jackson of JavaScript programmers.<br />

It’s important to note that the links you see in Figure 3-20 don’t actually exist in the HTML. JavaScript injects the three “A”<br />

links into the page once the page loads, ensuring that the controls are available only to users whose browsers can support<br />

them. And showing a near-compulsive obsession with the body element’s class, we can use that attribute to trigger our<br />

function, which will allow our content producers to drop a simple word into class to invoke the function, and thereby drop<br />

the links into the page.<br />

As always, let’s start with our body element—I know, shocking. But this time, let’s use text-sizer as our new class:<br />

<br />

Inside the scripts.js file we referenced from the head of our document is a function called buildTextSizer:<br />

<br />

There’s a fair amount of code to this function, so don’t worry if you’re not exactly a JavaScript ninja (after all, I’m certainly<br />

not). Thankfully, the part we’re most concerned with occurs right at the top:<br />

function buildTextSizer() {<br />

if (document.getElementsByTagName && document.createElement ➥<br />

&& document.getElementById) {<br />

var trigger = document.getElementsByTagName("body")[0];<br />

if (findWord("text-sizer", trigger.className)) {<br />

if (document.getElementById("article-content")) {<br />

var container = document.getElementById("article-content");<br />

} else {<br />

var container = document.getElementById("content-primary");<br />

}<br />

if (container) {<br />

// Build elements<br />

var slugs = new Array("small", "medium", "large");<br />

var controlContainer = document.createElement("div");<br />

var topList = document.createElement("ul");<br />

var innerList = document.createElement("ul");<br />

var listItem = document.createElement("li");<br />

var span = document.createElement("span");<br />

var labelText = document.createTextNode("Text Size:")<br />

// Loop over the text size "slugs", and build a link for<br />

//each one<br />

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

var text = document.createTextNode("A");<br />

var anchor = document.createElement("a");<br />

var item = document.createElement("li");<br />

anchor.appendChild(text);

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

Saved successfully!

Ooh no, something went wrong!