Chrome中的打印功能不再工作

Print function in Chrome no longer working

本文关键字:不再 工作 功能 打印 Chrome      更新时间:2023-09-26

我们的网站有一个功能,可以打印会员档案。它的工作方式是通过onsubmit将javascript函数附加到按钮上。javascript函数使用window.open以特殊模式重新打开页面,从而重新显示页面的打印机友好版本。

该功能自2008年左右开始使用,适用于所有浏览器。除了大约一周前,它已经停止在Chrome中工作了。对于Chrome,打开的窗口确实打开了,但随后另一个空白窗口短暂打开,然后所有窗口都关闭了。

在搜索关于这个问题的讨论时,我找不到确切的问题,但确实找到了一些内容,说应该在提交的内容中添加"return false"。我试着补充一下,但无济于事。

以下是onsubmit的样子:

<button onclick="PrintEmailSubmit('print');">Print Profile</button>

以下是打开窗口的代码:

window.open('print-email-profile.php?mode=' + mode,'','width=' + width + ',height=' + height + ',scrollbars=yes,location=0,menubar=1,status=0,toolbar=0')

虽然不需要查看,但以下是整个函数PrintEmailSubmit():

/*
 *  called by view-profile.php
 */
function PrintEmailSubmit(mode)
{
    var width;
    var height;
    switch(mode)
    {
        case 'print':
            width = 850;
            height = 1000;
            break;
        case 'email':
            width = 400;
            height = 120;
            break;
        default:
            alert('Error: invalid calling sequence -- should not happen!');
            exit;
    }
    window.open('print-email-profile.php?mode=' + mode,'','width=' + width + ',height=' + height + ',scrollbars=yes,location=0,menubar=1,status=0,toolbar=0');
}

最后,这个页面的特殊版本在body标签中添加了以下内容:

<body onload="window.print();window.close();">

如上所述,该功能在IE和Firefox中仍然有效。只是Chrome有这个问题。

有什么想法吗?

按钮和window.open实际上与您的问题无关。

问题是,Chrome在打印之前会寻找用户输入。Window.print()打开打印对话框窗口,但Chrome不会等待您完成打印。window.close()正在关闭打印对话框窗口和父窗口中的所有其他内容。

为了节省时间,请注意Chrome根本没有使用OnAfterPrint挂钩。我还试着在onLoad中放入window.close(),在onBeforeUnload中放入window.print(),但打印对话框取消了window.clos()

//In your profile print page
<html>
<head>
<script>
var is_chrome = function () { return Boolean(window.chrome); }
if(is_chrome) 
{
   window.print();
   setTimeout(function(){window.close();}, 10000); 
   //give them 10 seconds to print, then close
}
else
{
   window.print();
   window.close();
}
</script>
<body onLoad="loadHandler();">

我还没有对此进行测试,但我认为它相当有效地证明了这个想法。

我找到了这个解决方案,它真的很有效:

<body onload="window.print();window.onmouseover = function() { self.close(); } ">

我使用bresleveloper先生的解决方案,但有一些更改:

var is_chrome = Boolean(window.chrome);
if (is_chrome) {
    winPrint.onload = function () {
        setTimeout(function () { // wait until all resources loaded 
            winPrint.print();  // change window to winPrint
            winPrint.close();// change window to winPrint
        }, 200);
    };
}
else {
    winPrint.print();
    winPrint.close();
}

Chrome速度如此之快,以至于它实际上在加载文档之前就调用了打印。这就是为什么最好的方法就是将打印功能移动到onload,要么像Mari,要么像这样:

winPrint = window.open("", "winPrint", "width=1,height=1");
winPrint.document.write(html);
winPrint.onload = function () {
    window.print();
    window.close();
};

好吧,它在IE中不起作用,所以完整的代码应该看起来像这个

var is_chrome = Boolean(window.chrome);
if (is_chrome) {
    winPrint.onload = function () {
        window.print();
        window.close();
    };
}
else {
    winPrint.print();
    winPrint.close();
}
//In your profile print page
<html>
<head>
<script>
var is_chrome = function () { return Boolean(window.chrome); }
if(is_chrome) 
{
   window.print();
   setTimeout(function(){window.close();}, 10000); 
   //give them 10 seconds to print, then close
}
else
{
   window.print();
   window.close();
}
</script>
<body onLoad="loadHandler();">

这很好用。

谢谢。

我也遇到了与方法相同的问题

   window.print();
   window.close();

即使识别浏览器然后相应地执行代码也是好的。但这是一个非常方便和快捷的解决方案,只需两行即可解决此问题。

   document.execCommand('print');
   window.close();

chromeIE 中运行良好