WEB STANDARDS CREATIVITY
WEB STANDARDS CREATIVITY
WEB STANDARDS CREATIVITY
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
It is generally considered bad practice to use a link, stick an href of # in there, and then use the link for<br />
another purpose. So why the heck am I doing it? How can I justify these actions? Well, the reason that it’s<br />
normally a bad idea is that a link written this way fails when JavaScript is switched off. However, we’re using<br />
JavaScript to create this technique; therefore, there’s absolutely no danger of this situation happening.<br />
Another solution might be to dynamically write in buttons rather than a link. However, that itself presents<br />
some issues. First of all, this may limit your styling options (you have more CSS control over a link element<br />
than an input of type button or submit). Secondly, well, it’s not really a form is it? And if you dynamically<br />
write in form buttons, you need to wrap a form element around it. Also, if it is a form, should you use an<br />
input of type button or submit? No form data is being submitted for processing, after all. So, I opted for an<br />
anchor. Somebody shoot me!<br />
Finally, we call the Print dialog box. All the classnames have been changed—thus affecting their display—and so we’re pretty<br />
much good to go:<br />
window.print();<br />
}//end of the togglePrintDisplay function<br />
Writing the new links into the document<br />
We’re not quite done yet, though. The togglePrintDisplay function was called from the addPrintLinks function, and that<br />
first script wasn’t quite completed yet. There was the small matter of inserting the buttons (contained in paragraphs) into<br />
the web page after it loaded. That’s achieved with this line:<br />
el[i].insertBefore(newLinkPara,el[i].firstChild);<br />
Recap: what these scripts do<br />
And now we’re pretty much done. So to recap:<br />
1. The page loads, and addPrintLinks runs.<br />
2. addPrintLinks calls another function, togglePrintDisplay, where the behaviors are set.<br />
3. When the behavior is set for each element required, addPrintLinks resumes, appending the new links into the<br />
web page.<br />
Here are the finished scripts in their entirety:<br />
function addEvent(elm, evType, fn, useCapture)<br />
{<br />
if(elm.addEventListener)<br />
{<br />
elm.addEventListener(evType, fn, useCapture);<br />
return true;<br />
}<br />
else if (elm.attachEvent)<br />
{<br />
var r = elm.attachEvent('on' + evType, fn);<br />
return r;<br />
}<br />
else<br />
chapter 8 Print Magic: Using the DOM and CSS to Save the Planet<br />
195