7

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

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

在搜索这个问题的讨论时,我无法找到确切的问题,但确实找到了一些说“return false”应该添加到 onsubmit 的内容。我尝试添加它,但它没有帮助。

这是 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 有这个问题。

有任何想法吗?

4

6 回答 6

8

button 和 window.open 确实与您的问题无关。

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

为了节省您的时间,请注意 Chrome 根本不使用 OnAfterPrint 挂钩。我还尝试将window.close() 放在onLoad 中,将window.print() 放在onBeforeUnload 中,但是打印对话框取消了window.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();">

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

于 2013-08-14T19:08:52.277 回答
2

我找到了这个解决方案,它确实有效:

<body onload="window.print();window.onmouseover = function() { self.close(); } ">
于 2013-11-18T15:37:21.363 回答
1

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

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

而且 oc 它在 IE 中不起作用,所以完整的代码应该是这样的

var is_chrome = Boolean(window.chrome);
if (is_chrome) {
    winPrint.onload = function () {
        window.print();
        window.close();
    };
}
else {
    winPrint.print();
    winPrint.close();
}
于 2014-08-06T14:30:01.083 回答
1

我使用 Mr.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();
}
于 2014-12-24T08:21:58.150 回答
1
//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();">

这工作正常。

谢谢。

于 2019-11-06T18:30:49.270 回答
1

这种方法我也遇到同样的问题

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

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

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

chromeIE

于 2019-12-23T06:52:54.037 回答