I was trying to use JavaScript to replace the innerHTML of the receiving element on the template document with the innerHTML of the reference essay of the master document. To be blunt: I was unable to accomplish this. I did a LOT of Googling, and discovered that it simply wasn't possible to accomplish it in this manner. Therefore, I reverted to a very cumbersome method of implementing the docment.write method to create a whole new web page and then writing all the HTML for that page, including all the styling, etc. You can see the results here http://www.inlandrail.org/editorial_page.html. Just browse to any essay, and then press the "Printable version" button for that essay. The results are good, but as I mentioned, cumbersome. It would be great to have a fully formatted document that I could just pass the essay text to.
Here's where HTML5 shines. It includes the sessionStorage and localStorage methods, wherein you can write anything (any number of variables) to the sessionStorage or localStorage object, and then retrieve them later in any document that calls them. Sweet. I created an implementation scheme as follows:
1. In the "parent" document, when I press the "Printable version" button it calls the following script to parse out the appropriate essay and copyright specifier:
function print_essay(essay_to_print, print_copyright)
{
var name_to_print="print_" + essay_to_print;
sessionStorage.stuff_to_print=document.getElementById(name_to_print).innerHTML;
sessionStorage.copyrightyesno=print_copyright;
window.open('target_page-sessionStorage.html', '', 'left=0, width=800, height=600, menubar=yes, resizable=yes, scrollbars=yes, status=yes, toolbar=yes', '');
}
{
var name_to_print="print_" + essay_to_print;
sessionStorage.stuff_to_print=document.getElementById(name_to_print).innerHTML;
sessionStorage.copyrightyesno=print_copyright;
window.open('target_page-sessionStorage.html', '', 'left=0, width=800, height=600, menubar=yes, resizable=yes, scrollbars=yes, status=yes, toolbar=yes', '');
}
2. In the template document, I then retrieve the stored information using the following script called using <body onload="get_information()">:
function get_information()
{
//display the copyright, if called for
if (sessionStorage.copyrightyesno!=0)
{
document.getElementById("copyright_area").style.visibility="visible";
}
else
{
document.getElementById("copyright_area").style.visibility="hidden";
}
//insert the essay, itself
document.getElementById("essay_area").innerHTML=sessionStorage.stuff_to_print;
}
{
//display the copyright, if called for
if (sessionStorage.copyrightyesno!=0)
{
document.getElementById("copyright_area").style.visibility="visible";
}
else
{
document.getElementById("copyright_area").style.visibility="hidden";
}
//insert the essay, itself
document.getElementById("essay_area").innerHTML=sessionStorage.stuff_to_print;
}
The good news: it works GREAT in Chrome and Safari (these both use the WebKit engine). but fails in Firefox, IE8 and Opera. In IE8 I receive the following error: 'sessionStorage' is null or not an object. I've Googled MUCH to discover what I may have done or omitted, but continue to come up empty. I'll keep trying to figure it out.
The main purpose of this post was to share about this powerful feature of HTML5. And, you are probably aware there are many more. Things are definitely looking up!
If you have any idea as to why my code is not working in IE8, I'd love to hear about it.
Dick Raymond