打印页面作为弹出窗口不工作在chrome上工作在mozilla和IE

printing page as popup window is not working on chrome working on mozilla and IE

本文关键字:工作 chrome mozilla IE 窗口 打印      更新时间:2023-09-26

我正在尝试打印页面作为弹出窗口。对于Mozilla和IE,它可以工作,但在chrome上出现一个弹出窗口,但它显示"打印预览失败"。

恰好演示

javascript的一部分是-

function PrintElem(elem) {
    console.log($(elem).html())
     Popup($( elem).html());
}
function Popup(data)
{
    var printContent = data;
    var disp_setting="toolbar=no,location=no,directories=no,menubar=no, scrollbars=no,width=600, height=800"
    var myWindow = window.open("","",disp_setting);
    myWindow.document.write(printContent);
    myWindow.document.close();
    myWindow.focus();
    myWindow.print();
    myWindow.close();
    return true;
}

不知道为什么Chrome不满意我的脚本

setTimeout500毫秒工作时间:

function Popup(data)
{
  var printContent = data;
  var disp_setting="toolbar=no,location=no,directories=no,menubar=no, scrollbars=no,width=600, height=800";
  var myWindow = window.open("","",disp_setting);
  myWindow.document.open();
  myWindow.document.write(printContent);
  myWindow.document.close();
  myWindow.focus();
  setTimeout(function () {
    myWindow.print();
    myWindow.close();
  }, 500);
  return true;
}

如果你正在使用JQuery,你可以使用$(document).ready(function () {});。请看这里:

$(function () {
  window.print();
  window.close();
});

感谢A.Wolff的回答和VennilaSundarRajan对问题的见解我在这里发布代表这一点。真正的问题是按页渲染时间。使用这个——

myWindow.onload = function(){ this.print(); this.close(); }