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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9920397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:14:02  来源:igfitidea点击:

Window.onbeforeprint and Window.onafterprint get fired at the same time

javascripthtmleventsjavascript-events

提问by user581157

I have defined onbeforeprint and i modify my html code and now once i finish printing that is on select of print button i want the onafterprint to be fired but it does not.

我已经定义了 onbeforeprint 并修改了我的 html 代码,现在一旦我完成了选择打印按钮的打印,我希望 onafterprint 被触发,但它没有。

Instead when i press the Control + Print button the onbeforeprint is fired first and then the onafterprint event and then print dialog is shown .

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

Is there any way i could in some way do changes to my html after the "Print" button is clicked ?

单击“打印”按钮后,有什么办法可以以某种方式更改我的 html?

Am using IE -9 browser and the code is as follows Code

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

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

Thanks & Regards, Francis P.

感谢和问候,弗朗西斯 P。

采纳答案by kirilloid

onbeforeprintfired before dialog appears and allows one to change html and so on.

onbeforeprint在对话框出现之前触发并允许更改 html 等。

onafterprintis fired justbefore dialog appears. It is not even possible to know, whether document was actually printed or user canceled it. Needless to say about when printing finished (if started at all).

onafterprint被激发只是对话框出现之前。甚至无法知道文档是实际打印的还是用户取消的。不用说打印完成的时间(如果开始的话)。

Again: no event is available to track anything happened in print dialog, i.e. answer to your question is no.

再次:没有事件可用于跟踪打印对话框中发生的任何事情,即您的问题的答案是否定的

Moreover, I hope what your need will never be implemented, cause this allows to frustrate user. He/she asks to print one document, but got something different.

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

回答by Victor_Js

Yes, you can, no catch. I have thus implemented in a professional application. Print in Explorer, Firefox, all

是的,你可以,没有捕获。因此,我已在专业应用程序中实施。在资源管理器、Firefox 中打印,所有

window.onload = PrintMe;

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