umi*_*ght 48 javascript printing google-chrome new-window
在我工作的应用程序中,我们有几个用户可以打印的不同位置.在所有这些情况下,我们使用相同的工作流程打开一个新窗口(或选项卡),将我们需要打印的内容写入新窗口的文档,然后我们调用
$(w.document).ready(function () {
w.focus();
w.print();
w.close();
});
Run Code Online (Sandbox Code Playgroud)
我看到的问题是,在Chrome中,如果我关闭为打印预览而打开的标签页或窗口,而不是点击取消按钮,则Chrome仍会阻止我父窗口上的javascript.
它类似于此处描述的问题:
在子窗口上打开打印预览时,Google Chrome会阻止ajax请求
我们也遇到了这个问题,但我相信这是我们如何在新窗口中实现打印以及Chrome的打印预览工作方式的结果.在IE和Firefox中,打印窗口显示模式对话框,在打开窗口关闭之前,您无法在父窗口中执行任何操作.类似地,chrome阻止使用父窗口,直到取消打印预览.但是,我希望关闭该选项卡或窗口与取消打印相同.
有没有其他人有这个问题或知道一个好的解决方案?
谢谢!
小智 20
看起来这个问题已经通过最新的Chrome更新解决了......我正在运行Chrome版本36.0.1964.4 dev-m.
我也受到限制,通过执行以下操作警告用户关闭打印预览窗口:
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!\nPlease select "Stay on this Page option" and use the\nCancel button instead to close the Print Preview Window.\n';
}
}
window.print(); // Print preview
window.PPClose = true; // Set Close Flag to OK.
}
Run Code Online (Sandbox Code Playgroud)
现在,Chrome更新后警告不再出现.
问题是弹出窗口中存在浏览器内打印对话框。如果您window.close()立即致电,则用户看不到对话。用户需要在对话框中单击“打印”。这与其他打印浏览器是操作系统一部分的浏览器不同,在浏览器中,打印对话框window.close()一直被阻止,直到被关闭为止-在Chrome上,它是Chrome的一部分,而不是OS。
这是我在父窗口创建的弹出窗口中使用的代码:
var is_chrome = function () { return Boolean(window.chrome); }
window.onload = function() {
if(is_chrome){
/*
* These 2 lines are here because as usual, for other browsers,
* the window is a tiny 100x100 box that the user will barely see.
* On Chrome, it needs to be big enough for the dialogue to be read
* (NB, it also includes a page preview).
*/
window.moveTo(0,0);
window.resizeTo(640, 480);
// This line causes the print dialogue to appear, as usual:
window.print();
/*
* This setTimeout isn't fired until after .print() has finished
* or the dialogue is closed/cancelled.
* It doesn't need to be a big pause, 500ms seems OK.
*/
setTimeout(function(){
window.close();
}, 500);
} else {
// For other browsers we can do things more briefly:
window.print();
window.close();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
97115 次 |
| 最近记录: |