The onbeforeprint and onafterprint events of IE
The two printing-related events of IE are:
Note how both are events of the window, and can be defined using either the syntax above inside your script, or as HTML attributes, inside the BODY (ie: <body onbeforeprint="dothis()">) For example:
will alert two different messages, one proceeding and another after the document to be printed has been loaded into printer queue. Many people think that onafterprint is fired after a page has completed printing out, which is not true. Instead, as mentioned, it's fired when the printer has finished saving the document at the state it and sent to the printer queue.
With onbeforeprint in one hand and onafterprint in another, how we proceed to creating printer friendly pages depends entirely on the page at hand (whoops, that's three hands!), and the scripting techniques we choose to employ.
Here are a few examples to stimulate the mind in this respect.
-Changing a document's background image/color onprint
If your page's background is either of a dark color or an image, you know they can seriously guzzle your users' printer ink when the containing page is printed out. Be considerate to your loyal visitors, and toss em' out (the background, that is) on page print!
Nothing so fancy above that the comments inside can't explain.
-Removing sections of the page onprint
Considering the amount of window dressing most sites carry nowadays, the following example should be of interest to many of you. It removes sections of any page irrelevant to offline viewing before the page is fed to the printer.
Here's the basic idea: We enlist the help of CSS, or more precisely, the "display" property of style sheets, to toggle the existence of elements that should be removed. And to make this process generic, so we can easily specify different elements to remove, depending on the page the script is on, we use the "class" attribute of HTML to identify them.
Here is a script that will remove any section(s) of the page whose class attribute is set to "remove" when the page is printed out: