Chrome中的打印功能不再有效 [英] Print function in Chrome no longer working

查看:164
本文介绍了Chrome中的打印功能不再有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



此功能已存在关于2008年,并且适用于所有浏览器。除了大约一周前,它已停止在Chrome中工作。使用Chrome浏览器,会发生什么情况是打开的窗口会打开,然后短暂打开另一个空白窗口,然后全部关闭。



在搜索此讨论时问题我无法找到确切的问题,但确实发现了一些说应该在回执中添加退货错误的问题。



以下是onsubmit的内容:

 < button onclick =PrintEmailSubmit('print');>打印配置文件< / button> 

以下是打开窗口的代码:

  window.open('print-email-profile.php?mode ='+ mode,'','width ='+ width +',height ='+高度+',滚动条=是,位置= 0,菜单栏= 1,状态= 0,工具栏= 0')

虽然不需要看到,但这是整个函数PrintEmailSubmit():

  / * 
*由view-profile.php调用
* /
函数PrintEmailSubmit(mode)
{
var width;
var height;
switch(mode)
{
case'print':
width = 850;
height = 1000;
休息;

case'email':
width = 400;
height = 120;
休息;

默认值:
alert('错误:无效的调用序列 - 不应该发生!');
出口;
}
window.open('print-email-profile.php?mode ='+ mode,'','width ='+ width +',height ='+ height +',scrollbars =是,位置= 0,菜单栏= 1,状态= 0,工具栏= 0' );

$ / code>

最后,是什么使这项工作成为特殊版本的页面以下添加到body标签:

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

如上所述,该功能继续在IE和Firefox中运行。只是铬是有这个问题。



任何想法?

解决方案

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



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

为了节省您的时间,请注意Chrome根本不使用OnAfterPrint挂钩。我也试着在onBeoUnload和window.print()中放入window.close(),但打印对话框取消了window.close()。接下来最好的做法是:

  //在您的个人资料页面打印
< html>
< head>
< script>
var is_chrome = function(){return Boolean(window.chrome); }
if(is_chrome)
{
window.print();
setTimeout(function(){window.close();},10000);
//给他们10秒钟打印,然后关闭
}
else
{
window.print();
window.close();
}
< / script>

< body onLoad =loadHandler();>

我没有测试过这个,但我认为它可以很有效地证明这个想法。


Our website has a feature whereby a member profile can be printed. The way that it works is that a javascript function is attached to a button via an onsubmit. The javascript function uses a window.open to reopen the page in a special mode, which redisplays a printer-friendly version of the page.

This functionality has been in place since about 2008, and works in all browsers. Except about a week or so ago it has stopped working in Chrome. With Chrome, what happens is that the opened window does open, but then another blank window briefly opens, and then the all of them close.

In searching for discussion of this issue I was unable to find the exact issue, but did find something that said that a "return false" should be added to the onsubmit. I tried adding that but it did not help.

Here is what the onsubmit looks like:

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

Here is what the code that opens the window looks like:

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

While it should not be necessary to see, here is the entire function 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');
}

And finally, what makes this work is that the special version of the page has the following added to the body tag:

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

As stated above, the function continues to work in IE and Firefox. Just Chrome is having this issue.

Any ideas?

解决方案

The button and the window.open really have nothing to do with your problem.

The problem is, Chrome is looking for user input before it prints. Window.print() opens the print dialog window, but Chrome isn't waiting for you to finish printing. The window.close() is closing the print dialog window and everything else in the parent.

In an effort to save you time, be aware that the OnAfterPrint hook isn't used by Chrome at all. I also tried putting window.close() in the onLoad and window.print() in the onBeforeUnload, but the print dialog cancels the window.close(). The next best thing would be to do something like:

//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();">

I haven't tested this, but I think it demonstrates the idea fairly effectively.

这篇关于Chrome中的打印功能不再有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆