10

I have a page that calls window.print(); at the bottom of the page. I have no way of accessing the code around window.print(); Its generated by the server and I can't touch it. Basically because of IE I need to execute a bit of javascript before the print dialog comes up but after the page has loaded. I can't do this because as soon as it gets to window.print(); the print dialog comes up. I still need to print but first I need to run myFunction() then I can window.print();

<html><head></head><body></body><!--no access from here--><script>window.print();</script></html>
joe
  • 103
  • 1
  • 1
  • 4

5 Answers5

24

You should be able to override it like so...

var _print = window.print;
window.print = function() {
  alert("Hi!");
  // do stuff
  _print();
}
Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
  • I could've swore i tried that. That works perfectly though! Thanks! – joe Jul 21 '10 at 21:22
  • Awww, beat me to the punch! +1. – Pekka Jul 21 '10 at 21:23
  • @joe No problem, welcome to Stack Overflow! To "accept" this as the answer, you can click the checkmark under the vote down-arrow. – Josh Stodola Jul 21 '10 at 21:23
  • 1
    @Josh: not for another 5 minutes he can't :-) – Andy E Jul 21 '10 at 21:24
  • 9
    In Chrome 8 and other browsers, please note that using File · Print or otherwise executing print from browser chrome (not page scripts) does NOT call `window.print` and this code will not be executed. Test case: http://dl.dropbox.com/u/105727/web/print_function.html – Alan H. Jan 08 '11 at 02:09
  • 1
    @AlanH. Just tested with `Ctrl+P` in Chrome and I approve it doesn't work. – skmasq Aug 25 '13 at 21:07
  • Hi anyone. How can this code also show "Hi!" when printing directly from browser File. print. can someone point me to right direction please. – webs Feb 05 '18 at 00:38
10

"Basically because of IE I need to..."

If you only need support for IE, see the onbeforeprint event.

window.onbeforeprint = function () {
    // some code    
}

The benefit here is that the code will not run in browsers that don't support onbeforeprint, which is pretty much every browser except IE.

Andy E
  • 338,112
  • 86
  • 474
  • 445
2

This function works for all browsers including IE, no matter how the print is called:

/**
* Adds listeners for crossbrowser print callback
* @param callback - callback function
*/
function onPrint(callback) {
    window.matchMedia('print').addListener(query => query.matches ? callback() : null)
    window.addEventListener('beforeprint', () => callback())
}

onPrint(() => console.log('printing!'))

Be aware it may run the callback function twice, depending on the browser. This could be circumvented with a temporary flag.

Fabian von Ellerts
  • 4,763
  • 40
  • 35
0

Try putting another <script> tag before the one you don't have access to eg:

<html><head></head><body></body><script>alert('Hello There !');</script><script>window.print();</script></html>
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
0

A cleaner, more modern way of handling this:

window.addEventListener('beforeprint', () => {
    // before print, do something
});
Yes Barry
  • 9,514
  • 5
  • 50
  • 69