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.

192<br />

// create a container for the link to<br />

// go in and give it a class<br />

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

newLinkPara.setAttribute("class","printbutton");<br />

// set up the 'print this section' link<br />

newLink.setAttribute("href","#");<br />

// the print button will need a unique ID<br />

// (this will be used to tell the function<br />

// what section is to be shown or hidden )<br />

var btId = "printbut_" + el[i].id;<br />

newLink.setAttribute("id",btId);<br />

// add the text for the link to the anchor element<br />

newLink.appendChild(newLinkText);<br />

// add the anchor element to the paragraph element<br />

newLinkPara.appendChild(newLink);<br />

//add the behaviors for the new link<br />

newLink.onclick = togglePrintDisplay;<br />

newLink.onkeypress = togglePrintDisplay;<br />

}<br />

}<br />

}<br />

At this point, we’ve created the link that will be used to do the printout. It’s been placed in a paragraph, but that paragraph<br />

has not yet been inserted into the document. It’s currently in limbo—floating around in the browser’s memory somewhere—but<br />

we’ll be getting to that in just a moment. Carrying on with the pseudo-to-real code translations then:<br />

“The behavior [of the link] when clicked is to first check the id of the link that was clicked. If the id matches the current section<br />

(as the browser trawls through all the printable sections in the document), make that section visible (in the printed<br />

view). If the id of the link clicked does not match up (as the browser trawls through all the printable sections in the document),<br />

it hides the current section (again, only for print).<br />

Bring up the Print dialog box in the browser:”<br />

The behavior for the link was attached using these lines of script:<br />

newLink.onclick = togglePrintDisplay;<br />

newLink.onkeypress = togglePrintDisplay;<br />

This means that when the link is clicked or when the link has focus and a keypress event is detected, the browser should run<br />

a new function called togglePrintDisplay.<br />

Adding the behavior<br />

Let’s take a look at what’s in the togglePrintDisplay function. I’ll show it all in one go first, and then I’ll step through the<br />

constituent parts to explain what’s happening:<br />

function togglePrintDisplay(e)<br />

{<br />

var whatSection = this.id.split("_");<br />

whatSection = whatSection[1];<br />

var el = document.getElementsByTagName("div");

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

Saved successfully!

Ooh no, something went wrong!