Print Templates, Part II: TemplatePrinter: Detecting the Print Action - Doc JavaScript | WebReference

Print Templates, Part II: TemplatePrinter: Detecting the Print Action - Doc JavaScript


Print Templates, Part II: TemplatePrinter

Detecting the Print Action

The second page's LAYOUTRECT element has an event handler attached to it:

ONLAYOUTCOMPLETE="setTimeout('CheckIfPrintRequested()',
  100)"/>
</IE:DEVICERECT>

We want to call the function CheckIfPrintRequested() 100 milliseconds after the page is laid out. This is to ensure that the page won't be sent to the printer before the page's layout is complete and the event is consumed. A one millisecond interval would have been sufficient, but we want to play it safe.

The function CheckIfPrintRequested() gets its parameters from the dialogArguments variable. The dialogArguments is equal to the second parameter of the call to both showModalDialog() and showModelessDialog(). In this case, the executable printtemplates.exe calls the print template page, template2.html. This executable passes a few parameters as properties of a single object that is passed to the HTML page. The object is referenced in the print template as dialogArguments. Among its other properties, we use the __IE_PrintType to detect what the executable asked for. The executable determines its calls according to the user's requests through its Web page. When you click the Print with Prompt button, the executable sends a request to the Web page, setting __IE_PrintType to Prompt. The property __IE_PrintType may assume one of the following options: Prompt, Noprompt, and Preview. After checking the request, we call the proper function to handle it. We use a case statement for handling the various options:

invocations = 0;
function CheckIfPrintRequested() {
  invocations++;
  if (invocations > 1) return;
  switch (dialogArguments.__IE_PrintType) {
    case "Prompt":
      if (printer.showPrintDialog()) PrintPrep();
      break;
    case "NoPrompt":
      PrintPrep();
      break;
    case "Preview":
    default:
      break;
  }
}

Notice how we pop up the print dialog by calling one of the TEMPLATEPRINTER's methods, showPrintDialog(). Also notice the invocations variable. For some reason, the function CheckIfPrintRequested() is called twice when we request to print or preview the page. The variable invocations forces the switch statement to execute only once.

Next: How to print a document

http://www.internet.com


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: August 27, 2001
Revised: August 27, 2001

URL: http://www.webreference.com/js/column91/4.html