Window.onbeforeprint和Window.onafterprint同时被触发

时间:2012-03-29 06:50:03

标签: javascript html events javascript-events

我已经定义了onbeforeprint并且我修改了我的html代码,现在一旦我完成打印选择打印按钮,我希望onafterprint被解雇,但事实并非如此。

相反,当我按下Control + Print按钮时,首先触发onb​​eforeprint,然后显示onafterprint事件,然后显示打印对话框。

在"打印"之后,有什么办法可以在某种程度上改变我的HTML。单击按钮?

使用IE -9浏览器,代码如下 代码

<script type="text/javascript">
    window.onbeforeprint = function () {
        alert('Hello');
    }
    window.onafterprint = function () {
        alert('Bye');
    }
</script>

谢谢&amp;问候, Francis P。

3 个答案:

答案 0 :(得分:2)

在对话框出现之前触发了

onbeforeprint并允许人们更改html等等。

在对话框出现之前,

onafterprint会被解雇 。甚至不可能知道文件是否实际打印或用户取消它。不用说印刷什么时候完成(如果完全开始)。

同样:没有任何事件可用于跟踪打印对话框中发生的任何事情,即您的问题的回答是

此外,我希望永远不会实现您的需求,因为这会让用户感到沮丧。他/她要求打印一份文件,但有些不同。

答案 1 :(得分:2)

即使在现代浏览器中,我也尝试使用onafterprint事件时遇到了同样的问题。

基于这里的其他答案之一,我能够提出这个解决方案。让我在关闭打印对话框后关闭窗口:

// When the new window opens, immediately launch a print command,
// then queue up a window close action that will hang while the print dialog is still open.
// So far works in every browser tested(2020-09-22): IE/Chrome/Edge/Firefox
window.print();
setTimeout(function () {
    window.close(); // Replace this line with your own 'afterprint' logic.
}, 2000);

答案 2 :(得分:-1)

是的,你可以,没有抓住。因此我在专业应用程序中实现了。 在资源管理器,Firefox,所有

中打印
window.onload = PrintMe;

function PrintMe() {
    window.print();
    setTimeout(function () {
    alert("OK");
// Here you code, for example  __doPostBack('ReturnPrint', '');
    }, 2000);
}
相关问题