Hello,
I am trying to get some code working cross-browser and have run into some issues. At first I was using some code I found here:
http://www.webdeveloper.com/forum/showpost.php?p=860837&postcount=2
However, I have some users who are unable to print in IE8. Their browser freezes up with no error message when attempting to print using the above code. I am not entirely sure what the cause is, but I suspect it may have something to do with document.write/doctype/ie-bug.
The majority of my javascript is done in jquery so I thought I would look into a jquery-based solution. I have given this a go, but I’m having trouble creating a working solution without the document.write function. This is what I have right now, any suggestions on how I can get this print code to work in cross-browser (especially IE).
toPrint = function(id){
try{
var oIframe = $jq('#print-area');
var oContent = $jq(id).parent().html();
var oHead = $jq("head").html();
var oDoc = ($jq(oIframe)[0].contentWindow || $jq(oIframe)[0].contentDocument);
if ($jq(oDoc)[0].document) {
oDoc = $jq(oDoc)[0].document;
}
$jq(oDoc).html(getPrintStructure(oHead));
$jq("body",oDoc).append(oContent);
oDoc.close();
}
catch(e) {
self.print();
}
}
getPrintStructure = function(oHead){
structure += '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head>';
structure += oHead;
structure += "<style type='text/css' media='print'>"+
"body {margin:0; padding:0; line-height: 1.4em; word-spacing:1px; letter-spacing:0.2px; font: 13px Arial, Helvetica,'Lucida Grande', serif; color: #000;}"+
"</style>";
structure += "</head><body onload='this.focus(); this.print();'>";
structure += "</body></html>";
return structure;
};
The above code fails around:
$jq(oDoc).html(getPrintStructure(oHead));
Where I attempt to create the body structure for the iframe document.
Thanks for your help, i’ve stuck on this one for a while and not sure where to go next.