views:

59

answers:

2

Hi all, 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?

EDIT:

Still having some problems with this. I tried the putting my function in my Print.css as 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.

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().

+2  A: 

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

Wrikken
Are you suggesting something like this?http://www.siteexperts.com/forums/viewConverse.asp?d_id=19568
typoknig
Don't know if that'll work. I would suggest calling a server-sided script as the image source which indeed returns an image, and just does the stuff you like (log a print action, possibly set a session variable other js on the page can retrieve / poll for etc.)
Wrikken
A: 

Try masking the native window.print() with your own...

// 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.

no
Thanks @no, but I am having a hard time following your code. You say this will mask the native print function in any browser? This raises another question for me too, could I just make a JavaScript function that checked EVERY window event and then execute the function I want to execute before printing when `window.print == true`?
typoknig
@typoknig: it will; because of that you may want to choose different names for `onbeforeprint` and `onafterprint`. I'm assuming you want to catch calls to `window.print` from other scripts besides your own. About the second part of your question, I think it would work, but it sounds like overkill...
no
@typoknig: I added code comments to my answer, should help explain what's going on.
no
On my page I do not provide the user with a print button, so what I really need to catch is when the user goes to `File > Print` in a non IE browser. I'll mess around with it a bit and see if I can get your code to work, if not then I might go "overkill" :)
typoknig
This won't help you then. Take another look at Wrikken's answer; you can put the data you want into a query string, then add a print css rule that loads an image via a server-side script that stores the data from the query string before responding with an image.
no