Fires on the object just before its associated document prints.
Syntax
Inline HTML <ELEMENT onbeforeprint = "handler" ... > All platforms Event property object.onbeforeprint = handler JScript (compatible with ECMA 262 language specification) only Named script <SCRIPT FOR = object EVENT = onbeforeprint> Internet Explorer only
Remarks
Bubbles No Cancels No To invoke
- Choose Print from the File menu in Internet Explorer.
- Press CTRL + P.
- Right-click anywhere on a page, and choose Print.
- Right-click on a link on a page, and choose Print.
- From Windows Explorer, select an .htm file and choose Print from the File menu.
- From Windows Explorer, right-click on an .htm file and choose Print.
Default action Prints the document associated with the object for which the event is specified. This event gives the Web author an opportunity to modify the document just before it prints; in most cases, to make all the information on the page visible just before printing.
The event is usually used in conjunction with the onafterprint event, which provides a means for the Web author to undo the changes made to the document in the onbeforeprint event.
Event Object Properties
Although event handlers in the document object model do not receive parameters directly, the handler can query the event object for data.
Show Event Object Properties
altKey Retrieves the current state of the ALT key. button Retrieves which mouse button, if any, is pressed. cancelBubble Sets or retrieves whether the current event should bubble up the hierarchy of event handlers. clientX Retrieves the x-coordinate of the position of the cursor when the mouse is clicked, relative to the size of the client area of the window but excluding window decorations or scroll bars. clientY Returns the y-coordinate of the position of the cursor when the mouse is clicked, relative to the size of the client area of the window but excluding window decorations or scroll bars. ctrlKey Retrieves the state of the CTRL key. returnValue Sets or retrieves the return value from the event. screenX Retrieves the horizontal position of the mouse, in pixels, relative to the user's screen. screenY Retrieves the vertical position of the mouse, in pixels, relative to the user's screen. shiftKey Retrieves the state of the SHIFT key. srcElement Retrieves the object that fired the event. type Retrieves the event name from the event object. x Returns the horizontal position of the mouse when the event fires. y Returns the vertical position of the mouse when the event fires.
Example
The following example demonstrates how the onbeforeprint event can be used to make all currently hidden sections of the page visible just before the page prints. The onafterprint event is then processed, after the page prints, to revert the document back to its original state.
Show Sample Code
function window.onbeforeprint() { // walk through all the elements in the document with CLASS= "clsCollapsed" // and set it to "expanded" just for printing. var coll = document.all.tags("DIV"); if (coll!=null) { for (i=0; i<coll.length; i++) if (coll[i].className == "clsCollapsed") { coll[i].className = "clsExpanded"; // we want to make sure that after printing, // we set CLASS="clsCollapsed" only for those that we // expanded just for printing purposes. coll[i].bExpandedForPrinting = true; } else if (coll[i].className == "clsExpanded") coll[i].bExpandedForPrinting = false; } } function window.onafterprint() { // walk through all the elements in the doc with CLASS="clsExpanded" // and set it back to "clsCollapsed" if expanded just for printing purposes. var coll = document.all.tags("DIV"); if (coll!=null) { for (i=0; i<coll.length; i++) if ((coll[i].className == "clsExpanded") && (coll[i].bExpandedForPrinting)) { coll[i].className = "clsCollapsed"; coll[i].bExpandedForPrinting = false; } } }
See Also
[Right-click and choose View Source from the shortcut menu.]
© 2007 Microsoft Corporation. All rights reserved. Terms of use.