javascript onbeforeprint() 和 onafterprint() 等价于非 IE 浏览器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3339789/
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
onbeforeprint() and onafterprint() equivalent for non IE browsers
提问by ubiquibacon
I want to send some info back to my database when a user prints a certain web page. I can do this in IE with onbeforeprint()and onafterprint()but I would like to browser agnostic way of doing the same thing. Don't care which combination of technologies I have to use (PHP, MySQL, JavaScript, HTML) so long as it gets done. Any ideas?
当用户打印某个网页时,我想将一些信息发送回我的数据库。我可以在 IE 中使用onbeforeprint()和onafterprint()但我想以浏览器不可知的方式做同样的事情。只要完成,我就不必在意我必须使用哪种技术组合(PHP、MySQL、JavaScript、HTML)。有任何想法吗?
EDIT:
编辑:
Still having some problems with this. I tried the putting my function in my Print.cssas an image, but I am messing it up some how. Then I tried just adding a event listener, but I cannot get that to work quite right either. If anyone can provide some more details on how I might call a function right before print in ANY browser I would appreciate it.
仍然有一些问题。我尝试将我的函数Print.css作为图像放入我的函数中,但我在某些方面弄乱了它。然后我尝试只添加一个事件侦听器,但我也无法让它正常工作。如果有人能提供更多关于我如何在任何浏览器中打印之前调用函数的详细信息,我将不胜感激。
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().
我现在放弃这个,我已经解决了另一种做我想做的事情。我期待 FireFox 支持 onbeforeprint() 和 onafterprint() 的那一天。
采纳答案by Wrikken
I m not sure other browsers will allow you to. You could of course specify an image somewhere in a print stylesheet, which probably only will be called on a print, for the onbeforeprint
我不确定其他浏览器会允许您这样做。您当然可以在打印样式表的某处指定一个图像,它可能只会在打印时调用,因为onbeforeprint
回答by quietmint
Many browsersnow support window.matchMedia. This API allows you to detect when CSS media queries go into effect (e.g., rotating the screen or printing the document). For a cross-browser approach, combine window.matchMediawith window.onbeforeprint/window.onafterprint.
现在很多浏览器都支持window.matchMedia. 此 API 允许您检测 CSS 媒体查询何时生效(例如,旋转屏幕或打印文档)。对于一个跨浏览器的方式,结合window.matchMedia带window.onbeforeprint/ window.onafterprint。
The following may result in multiple calls to beforePrint()and afterPrint()(for example, Chrome fires the listener every time the print preview is regenerated). This may or may not be desirable depending on the particular processing you're doing in response to the print.
以下可能会导致多次调用beforePrint()and afterPrint()(例如,每次重新生成打印预览时,Chrome 都会触发侦听器)。这可能是也可能不是可取的,这取决于您为响应打印而进行的特定处理。
if ('matchMedia' in window) {
// Chrome, Firefox, and IE 10 support mediaMatch listeners
window.matchMedia('print').addListener(function(media) {
if (media.matches) {
beforePrint();
} else {
// Fires immediately, so wait for the first mouse movement
$(document).one('mouseover', afterPrint);
}
});
} else {
// IE and Firefox fire before/after events
$(window).on('beforeprint', beforePrint);
$(window).on('afterprint', afterPrint);
}
More: http://tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/
更多:http: //tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/
回答by Dagg Nabbit
Try masking the native window.print()with your own...
尝试window.print()用你自己的掩饰本机...
// hide our vars from the global scope
(function(){
// make a copy of the native window.print
var _print = this.print;
// create a new window.print
this.print = function () {
// if `onbeforeprint` exists, call it.
if (this.onbeforeprint) onbeforeprint(this);
// call the original `window.print`.
_print();
// if `onafterprint` exists, call it.
if (this.onafterprint) onafterprint(this);
}
}())
Updated: comments.
更新:评论。
回答by userfuser
I think that it's simply not possible to this properly. Or at least - not with any technology I know nor with any of the answers given previously.
我认为这是不可能的。或者至少 - 不是使用我所知道的任何技术,也不是使用之前给出的任何答案。
Both using onafterprint and using serverside dynamic-image-generating script would tell you that the page was printed even when the visitor merely went to print preview mode and then canceled out.
使用 onafterprint 和使用服务器端动态图像生成脚本都会告诉您,即使访问者只是进入打印预览模式然后取消,页面也已打印。
However, I would like to learn how to get the proper information, so that I can be surethat page was actually printed.
但是,我想了解如何获取正确的信息,以确保该页面确实已打印。

