Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining

beforeprint | onbeforeprint event

Fires on the object before its associated document prints or previews for printing.


HTML Attribute <element onbeforeprint = "handler(event)">
Event Property object.onbeforeprint = handler;
attachEvent Method object.attachEvent("onbeforeprint", handler)
addEventListener Method object.addEventListener("beforeprint", handler, useCapture)


Event information



Event handler parameters

pEvtObj [in]

Type: IHTMLEventObj

Standards information

There are no standards that apply here.


Use this event to modify the document just before it prints or previews for printing. In most cases it is used to make all the information on the document visible just before printing.

Use the event in conjunction with the onafterprint event to undo the changes made to the document in the onbeforeprint event.

Prints the document associated with the object for which the event is specified.

To invoke this event, do one of the following:

  • Choose Print or Print Preview from the File menu.
  • Press CTRL+P.
  • Right-click anywhere on a document, and choose Print.
  • Right-click a link on a document, and choose Print.
  • From Windows Explorer, select an .htm file and choose Print from the File menu.
  • From Windows Explorer, right-click an .htm file and choose Print.

The pEvtObj parameter is required for the following interfaces:


This example uses the onbeforeprint to make all hidden sections of the document visible just before the document prints. The onafterprint event is processed after the document prints to return the document to its original state.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onbeforeprint.htm

function window.onbeforeprint()
    // Walk through all the elements in the document with
    // CLASS="collapsed" 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 == "collapsed")
           coll[i].className = "expanded";
        // After printing, make sure to set CLASS="collapsed" 
        // only for those that were expanded just for printing.
           coll[i].bExpandedForPrinting = true;
         else if (coll[i].className == "expanded")
            coll[i].bExpandedForPrinting = false;
function window.onafterprint()
   // Walk through all the elements in the doc with CLASS="expanded"
   // and set it to "collapsed" if expanded just for 
   // printing.
   var coll = document.all.tags("DIV");
   if (coll!=null)
      for (i=0; i < coll.length; i++) 
         if ((coll[i].className == "expanded") &&
           coll[i].className = "collapsed";
           coll[i].bExpandedForPrinting = false;

See also




© 2015 Microsoft