views:

82

answers:

1

Hi all, I am trying to work through this question and I have had little success tonight. I think I can make the code below work if I only knew what event was fired when the window.print() function is called.

var browser_name = navigator.appName; 

if(browser_name == 'Microsoft Internet Explorer'){
     window.attachEvent("print()",on_print_function);//I realize I cannot attach an event listener to a function, I just wanted you to see what I am trying to accomplish
}

else{
     window.addEventListener("print()",on_print_function,false);
}

The function that is called when the print event takes place returns a page that stores some info in the database.

My end objective is to have the page print ONLY if the info in question is going to be stored in the database. I am open to better ways of tackling this, but I think I will have it going ok if I can just get the event for the print() as I said.

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

+1  A: 

Well as far as I know, IE has several events line onbeforeprint() and onafterprint() but they are not supported by other browsers. So relying on this is not very good.

Perhaps you can have a print button on your page. Attach to it a handler which executes the ajax call to the server to store the data to the database and on success of this call, call the print() on the window.

Is that what you are looking for ?

sTodorov
A print button is an option, but I really want to catch when people go to `File > Print`. The function `onbeforeprint()` would work for me if I was only concerned with IE, but I need this to work in FireFox too. My other question has so background info:http://stackoverflow.com/questions/3339789/onbeforeprint-and-onafterprint-equivalent-for-non-ie-browsers-php-mysql-ja
typoknig
Well as far as I know other browsers do not support those events and you will have issues working with them. Perhaps, a slightly different approach. Is this data a form on the page? Can this data be submitted onpageload? Is it possible to navigate the user to the page for printing after the submit button on the page?
sTodorov
The problem is that the page that I am dealing with is a series of customer bills which will be automatically mailed when printed. The page is just a review if the user does not print the page, but if they do print the page then the database needs to be updated to say that the bills were printed and have been mailed. I cannot take the chance of the user printing the bills and the database not getting updated. I need them to happen one right after the other.
typoknig
Another approach I can think of, although a bit of a hack is to hide the divs showing the bills using a print stylesheet, and thus enforce the user to click on your print button.
sTodorov