Nir
Nir

Reputation: 2629

window.print() javascript doesnt get previewed in chrome

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.org/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...

Upvotes: 2

Views: 10230

Answers (2)

personaelit
personaelit

Reputation: 1653

I 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.net 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>

Upvotes: 3

James Montagne
James Montagne

Reputation: 78630

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.

Upvotes: 0

Related Questions