What event is fired when window.print() is called?

不羁的心 提交于 2019-12-22 18:54:06

问题


I am trying to work through this question and I have had little success tonight. I think I can make the code below work if I only knew what event was fired when the window.print() function is called.

var browser_name = navigator.appName; 

if(browser_name == 'Microsoft Internet Explorer'){
     window.attachEvent("print()",on_print_function);//I realize I cannot attach an event listener to a function, I just wanted you to see what I am trying to accomplish
}

else{
     window.addEventListener("print()",on_print_function,false);
}

The function that is called when the print event takes place returns a page that stores some info in the database.

My end objective is to have the page print ONLY if the info in question is going to be stored in the database. I am open to better ways of tackling this, but I think I will have it going ok if I can just get the event for the print() as I said.

EDIT

I am giving up on this for now, I have settled with another way of doing what I want. I look forward to the day when FireFox supports onbeforeprint() and onafterprint().


回答1:


Well as far as I know, IE has several events line onbeforeprint() and onafterprint() but they are not supported by other browsers. So relying on this is not very good.

Perhaps you can have a print button on your page. Attach to it a handler which executes the ajax call to the server to store the data to the database and on success of this call, call the print() on the window.

Is that what you are looking for ?




回答2:


Apparently you can now listen for this with Firefox (but not other browsers):

Some browsers (including Firefox 6 and later and Internet Explorer) send beforeprint and afterprint events to let content determine when printing may have occurred. You can use this to adjust the user interface presented during printing (such as by displaying or hiding user interface elements during the print process).

The afterprint event is raised after the user prints or aborts a print dialog.

https://developer.mozilla.org/en-US/docs/Web/API/window.onafterprint

You may be able to emulate this with media queries in other browsers: https://stackoverflow.com/a/18325463/421243



来源:https://stackoverflow.com/questions/3340862/what-event-is-fired-when-window-print-is-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!