views:

145

answers:

2

Is there a way to abort the print event after a user has pressed the web browsers print button?

This works for print buttons within the HTML (jQuery):

print_btn.click(function() {
    if (confirm("Are you sure you want to print? Consider the environment")) {
        window.print();
    }
});

Is there a way to do the same with the web browsers print button? For IE there seemes to be a onbeforeprint event, but from there I can't find a way to abort the printing.

Thanks!

+2  A: 

Have you tried to return false; in IE's onbeforeprint event?

I doubt it's possible in any other browsers but IE. Having said that, it wouldn't surprise me if it worked in IE using the onbeforeprint event. Shocking, I know.

Wim Hollebrandse
It didn't work.
pthulin
Well, can't be done then. Don't forget that all the JavaScript code runs inside its own sandbox, it would be a security risk if you had any control (even cancelling) over built-in functions like Print, File Save etc. of the hosting browser.
Wim Hollebrandse
+3  A: 

This isn't possible in general, and honestly I'm quite glad that's the case.

You could, however, do something dirty like make a style sheet that uses media selectors to make everything display: none; when printing, or something similar. I haven't ever tried this but it seems totally within spec (if completely pretentious).

Cory Petosky
wouldn't help for the page title, page numbering etc. that browsers are inserting.
Piskvor
Sure, but if his goal is really to "consider the environment", at least he'd save a marginal amount of ink. :)
Cory Petosky