最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

image - window.print() javascript doesnt get previewed in chrome - Stack Overflow

programmeradmin5浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 3

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

发布评论

评论列表(0)

  1. 暂无评论