When I try to open and print a window usingJavaScript
's window.print(), the image doesn't show in Chrome
, when it prints. My code is below:
<html>
<head>
<script type="text/javascript">
function openWin()
{
var myWindow = window.open('','', 'width=200,height=100');
myWindow.document.write("<p>This is 'myWindow'</p>");
myWindow.document.write("<p><img src='.png' width='400' /></p>");
myWindow.document.close();
myWindow.focus();
myWindow.print();
myWindow.close();
}
</script>
</head>
<body>
<input type="button" value="Open window" onclick="openWin()" />
</body>
</html>
And you can check it here / How can fix this?
When I try to open and print a window usingJavaScript
's window.print(), the image doesn't show in Chrome
, when it prints. My code is below:
<html>
<head>
<script type="text/javascript">
function openWin()
{
var myWindow = window.open('','', 'width=200,height=100');
myWindow.document.write("<p>This is 'myWindow'</p>");
myWindow.document.write("<p><img src='https://www.google.gr/images/srpr/logo11w.png' width='400' /></p>");
myWindow.document.close();
myWindow.focus();
myWindow.print();
myWindow.close();
}
</script>
</head>
<body>
<input type="button" value="Open window" onclick="openWin()" />
</body>
</html>
And you can check it here http://jsfiddle/Q5Xc9/649/ How can fix this?
Share Improve this question edited Feb 23, 2014 at 14:07 0xcaff 13.7k5 gold badges51 silver badges57 bronze badges asked Jan 3, 2014 at 12:22 A. ZalonisA. Zalonis 1,6096 gold badges27 silver badges42 bronze badges1 Answer
Reset to default 5The image hasn't been loaded when the call to print()
is made.
You need to delay the call to print
until AFTER the image is finished loading. jQuery can do this, so you could include jquery in the page, and then wrap the call to print
in code like this:
function openWin()
{
var myWindow = window.open('','','width=500,height=500');
myWindow.document.write("<p>This is 'myWindow'</p>");
myWindow.document.write("<p><img src='https://www.google.gr/images/srpr/logo11w.png' width='400' /> </p>");
myWindow.document.write("<script src='http://code.jquery./jquery-1.10.1.min.js'></script>");
myWindow.document.write("<script>$(window).load(function(){ print(); });</script>");
}