I have a lightbox plugin that required additional option - print the image. So I did a little JS function that print that image as follow:
var headstr="<!DOCUMENT html><html xmlns=''><head><title></title></head><body>";
var footstr="</body>";
var newstr="<img src=\"[img-location]\" alt='courses' />";
document.body.innerHTML=headstr+newstr+footstr;
window.print();
window.location.reload();
The problem is that when the user press on the print button, in chrome it opens a new window (chrome print page) and in it, it says - print preview failed. In firefox and IE8 it works just fine...
I have a lightbox plugin that required additional option - print the image. So I did a little JS function that print that image as follow:
var headstr="<!DOCUMENT html><html xmlns='http://www.w3/1999/xhtml'><head><title></title></head><body>";
var footstr="</body>";
var newstr="<img src=\"[img-location]\" alt='courses' />";
document.body.innerHTML=headstr+newstr+footstr;
window.print();
window.location.reload();
The problem is that when the user press on the print button, in chrome it opens a new window (chrome print page) and in it, it says - print preview failed. In firefox and IE8 it works just fine...
Share Improve this question edited Nov 12, 2013 at 0:26 BenMorel 36.7k52 gold badges206 silver badges337 bronze badges asked Aug 4, 2011 at 18:06 NirNir 2,6579 gold badges48 silver badges75 bronze badges2 Answers
Reset to default 3I don't know if this is what caused your failure, but if window.print() is called from an <input type="submit"> within a form with method="post" (i.e. every asp page ever) then Chrome print preview wigs out.
The solution to this is to add return false; after window.print() so that the page doesn't post back.
<html>
<head>
</head>
<body>
<form method="post" action="test.html">
<p>This is only a test. Had this been a real emergency....</p>
<!--This doesn't work -->
<!-- <input type="submit" onclick="JavaScript:window.print();">-->
<!--But this does -->
<input type="submit" onclick="JavaScript:window.print();return false;">
</form>
</body>
</html>
I'm not sure if this is the problem, but you're creating invalid html. For one, you don't close the <html>
tag. In addition, you're putting an html
and head
tag within your body.