Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detect browser print (ctrl+p) using jquery

I have a button on my page called "Print". clicking on which will trigger a jQuery function.

I need to call the same jQuery function when user hits Alt+f+p or ctrl+p

How do I do that?

I tried to do that with matchMedia but no luck

 if (window.matchMedia('print').matches) {
        alert("print using browser");
    }
like image 933
BumbleBee Avatar asked Apr 25 '26 19:04

BumbleBee


1 Answers

There is not a standard way to do that as far as I know.

IE and FF offer own function calls,

In IE there are the nonstandard window.onBeforePrint() and window.onAfterPrint() event listeners. There isn't a non-IE way to do it that I know of, however.

Firefox 6 now supports beforeprint and afterprint https://developer.mozilla.org/en/Printing#Detecting_print_requests

A universally working solution might be to just listen for CMD+P.

like image 157
poitroae Avatar answered Apr 27 '26 10:04

poitroae



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!