在新窗口中打开一个新页面,打印并关闭它

Open a new page in a new window, print it and closed it

本文关键字:打印 新页面 一个 在新窗口中打开      更新时间:2023-09-26

我仍然不能得到这个代码的工作,我试图使用"onClick"交换图像一旦点击,在一个新的窗口打开这个新页面,打印这个新打开的窗口,并关闭它。它在新窗口打开了新页面,但是打印了一个空白页面,并且无法关闭它,似乎焦点没有被设置。有谁能帮我一下吗?谢谢!

<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
 <title>Print it</title>
<!--style type="text/css" media="print"> .noprint {visibility: hidden;} </style-->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$(".img-swap").live('click', function() {
if ($(this).attr("class") == "img-swap") {
  this.src = this.src.replace("_off","_on");
} else {
  this.src = this.src.replace("_on","_off");
}
$(this).toggleClass("on");
});
});
</script>
<script type="text/javascript">
// <!--
 Popup = {
 init : function () {
 $('a.action_print').bind('click', Popup.printIt);
    },
     printIt : function () {
        var win = window.open('doc1.html','Printed','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=350,height=250');
        if (win) {
            win.document.close();
             win.focus();
            win.print();
        }
        return false;
    }
}
$(document).ready(function () {
    Popup.init();
});

 // -->
</script>
 </head>
 <body>
 <br>
 CLick to Print:<br><br>
<p style=" padding: 125px 0; text-align: center;">
<a class="action_print" target="Printed"><img src="images/cam_off.png" class="img-swap" width="100" height="100" /></a>
</p>
</body>
</html>

感谢您的关注!

我弄清楚了,所有工作,我只需要确保我打印的文件离开在同一个域。我希望它能帮助其他需要这样的东西,但任何对代码的改进都是受欢迎的。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Open n Print</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type='text/javascript'>//<![CDATA[ 
 $(function(){
  function openPrintWindow(url, name, specs) {
    var printWindow = window.open(url, name, specs);
    var printAndClose = function () {
      if (printWindow.document.readyState == 'complete') {
        clearInterval(sched);
        printWindow.print();
        printWindow.close();
     }
    }  
      var sched = setInterval(printAndClose, 200);
   };
 jQuery(document).ready(function ($) {
   $(".test").on("click", function (e) {
    var myUrl = $(this).attr('data-url');
    alert(myUrl);
    e.preventDefault();
    openPrintWindow(myUrl, "to_print", "width=700,height=400,_blank");
   });
 });
 });//]]>  
  </script>
 </head>
 <body>
  </br>
  <a class="test" href="javascript:;" data-url= "doc.html">open print and close doc</a>
  <br />
  </body>
 </html>

printIt看起来很奇怪。首先关闭document,然后尝试打印内容。试试这个:

if (win) {
    win.focus();
    win.print();
    win.close();
}

关闭document不会关闭window,因此win.close()。还要注意行顺序。

另外,jQuery 1.4相当旧,你考虑过更新到一个新版本吗?

你可以把你的javascript/JQuery代码放在我放window.print();

 ScriptManager.RegisterStartupScript(this, typeof(Page), "UpdateMsg", "window.print();", true);

要让窗口的全部内容正确加载,请更改

printWindow.close()

printWindow.onfocus=function(){ printWindow.close();}

参见打印后自动关闭窗口对话框关闭