Multiple Javascript/Print standards in one example
This javascript example of dymanically creating a "Print this page" link and printing the page passes the following standards.
- 7.4 Web pages are able to be printed in whole
- Allows for printing of the whole web page
- 13.1 Pages usable when scripts, applets and other programmatic objects turned off
- Printing the page is still available as a browser function
- The script should degrade gracefully if the users browser does not support key event handlers
- 13.2 Alternative event handlers & device dependence
- The script does not require a specific device (i.e a mouse) for the script to function
- 15.2 Unique interfacing and device-independence
- Similarly to 13.2, the script does not require a specific device (i.e a mouse) for the script to function
Javascript
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
if (document.getElementById){
var print_page = document.getElementById('nav');
// create print link
var print_function = document.createElement('p');
var print_function_link = document.createElement('a');
print_function_link.onclick = function(){ print_button(); return false; }
print_function_link.setAttribute('href', '#content');
var print_function_link_text = document.createTextNode('Print this Page');
print_function_link.appendChild(print_function_link_text);
print_function.appendChild(print_function_link);
print_page.appendChild(print_function);
}
});
function print_button() {
// Print the page
window.print();
}