javascript window.print() 在 chrome 中,关闭新窗口或选项卡而不是取消打印使 javascript 在父窗口中被阻止 [英] Javascript window.print() in chrome, closing new window or tab instead of cancelling print leaves javascript blocked in parent window

查看:37
本文介绍了javascript window.print() 在 chrome 中,关闭新窗口或选项卡而不是取消打印使 javascript 在父窗口中被阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我处理的应用程序中,我们有几个不同的地方可供用户打印.在所有这些情况下,我们使用相同的工作流程打开一个新窗口(或选项卡),将我们需要打印的任何内容写入新窗口的文档,然后我们调用

In the application I work on, we have several different places a user can print from. In all these cases we are using the same workflow of opening a new window(or tab), writing whatever we need to print to the document of the new window, and then we call

    $(w.document).ready(function () {
        w.focus();
        w.print();
        w.close();
    });  

我看到的问题是,在 Chrome 中,如果我关闭为打印预览打开的选项卡或窗口而不是单击取消按钮,Chrome 仍会阻止父窗口上的 javascript.

The issue I'm seeing is that in Chrome, if I close the tab or window that is opened for the print preview instead of clicking the cancel button, Chrome is still blocking the javascript on my parent window.

它类似于此处描述的问题:

谷歌浏览器在子窗口上打开打印预览时阻止 ajax 请求

我们也遇到了这个问题,但我相信这是我们如何在新窗口中实现打印以及 Chrome 打印预览的工作方式的结果.在 IE 和 Firefox 中,打印窗口显示模态对话框,并且在关闭打印窗口之前您无法在父窗口中执行任何操作.类似地,chrome 会阻止使用父窗口,直到取消打印预览.但是,我希望关闭该选项卡或窗口的工作方式与取消打印相同.
有没有其他人遇到过这个问题或知道一个好的解决方案?

It is similar to the issue described here:

Google Chrome blocks ajax requests when print preview is opened on child window

We are experiencing this issue as well, but I believe this is a result of how we are implementing printing in a new window and the way Chrome's print preview works. In IE and Firefox, the print window displays the modal dialog, and you are not able to do anything in the parent window until the print window is closed. Similarly chrome is blocking use of the parent window until the print preview is cancelled. However I would expect closing that tab or window to work the same as cancelling the print.
Has anyone else had this issue or know of a good solution?

谢谢!

推荐答案

看来问题已通过最新的 Chrome 更新解决...我正在运行 Chrome 版本 36.0.1964.4 dev-m.

It looks like the problem had been resolved with the latest Chrome update... I'm running the Chrome Version 36.0.1964.4 dev-m.

我通过执行以下操作来警告用户不要关闭打印预览窗口:

I was limited too warning the user from closing print preview window by doing the following:

if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1){   // Chrome Browser Detected?
    window.PPClose = false;                                     // Clear Close Flag
    window.onbeforeunload = function(){                         // Before Window Close Event
        if(window.PPClose === false){                           // Close not OK?
            return 'Leaving this page will block the parent window!
Please select "Stay on this Page option" and use the
Cancel button instead to close the Print Preview Window.
';
        }
    }                   
    window.print();                                             // Print preview
    window.PPClose = true;                                      // Set Close Flag to OK.
}

现在 Chrome 更新后不再出现警告.

Now the warning is no longer coming up after the Chrome update.

这篇关于javascript window.print() 在 chrome 中,关闭新窗口或选项卡而不是取消打印使 javascript 在父窗口中被阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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