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

html - Create and Open new Window and Print Images using JavaScript and Google Chrome - Stack Overflow

programmeradmin1浏览0评论

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

1 Answer 1

Reset to default 5

The 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>");
}
发布评论

评论列表(0)

  1. 暂无评论