Home     My Faqts     Contributors     About     Help    

faqts : Computers : Programming : Languages : JavaScript : Event handling

FAQTs repaired & updated!
Thanks for your patience...
Entry Add Entry Alert - Edit this Entry

Did You Find This Entry Useful?

123 of 144 people (85%) answered Yes
Recently 7 of 10 people (70%) answered Yes

How to print a web page directly with out having a Printer Dialog ?? like the print button, does in the browser

How can I print without previewing or displaying first? ie. I want to have a "report selection crite

May 22nd, 2002 09:13

jsWalter, Colin Fraser, Chamila Mihiripenna, Dan Hardison


Yes, there are 2 ways to do this...
  1) Print Button on Browser Toolbar
  2) custom code if toolbar is not there
The code as stated previous *will* display the Print Dialog.
(Please, always test code before posting to this forum)
This is the code that will print a given window/frame *without* the 
Dialog.
Walter
// ====================================================================
// Original post: Unkown person - I lost my notes on who did this first
//                Unkown source
// Modified by:   Walter Torres <> [www.torres.ws]
//                4/29/2001
//                I found the secret to remove the prompt!
//                Original post did not have this gem to it.
//
// This accesses a built-in Windows command that can perform Magic!
// And yes, this is a Windows ONLY solution.
// In fact, it only works in IE. :(
//
//          INPUT: intOLEcmd   = integer between 1 and 37,only a few 
are of use
//                 intOLEparam = parameter integer for function - 
optional
//         OUTPUT: none
//   DEPENDANCIES: none
//
//           NOTE: intOLEparam is not optional in the Object call,
//                 I just made it optional here to make life easier.
//                 All command values use '1' execept print, thus my 
reasoning.
//
//        EXAMPLE: // This prints given window/frame WITHOUT prompt!
//                 <button onClick="objWinName.ieExecWB(6, -1)">
//                    Print Me! - No Prompt!
//                 </button>
//
//                 // This prints given window/frame WITH prompt!
//                 <button onClick="objWinName.ieExecWB(6)">
//                    Print Me! - Prompt
//                 </button>
//
// 	               // This will display the Print Preview window
//                 <button onClick="objWinName.ieExecWB(7)">
//                    Print Preview
//                 </button>
//
//         VALUES: intOLEcmd has these possible values
//                 OLECMDID_OPEN         = 1
//                 OLECMDID_NEW          = 2    warning, this kills IE 
windows!
//                 OLECMDID_SAVE         = 3
//                 OLECMDID_SAVEAS       = 4
//                 OLECMDID_SAVECOPYAS   = 5    note: does nothing in IE
//                 OLECMDID_PRINT        = 6    note: give '-1' as 
param - no prompt!
//                 OLECMDID_PRINTPREVIEW = 7
//                 OLECMDID_PAGESETUP    = 8
//                          Others have no use in IE
function ieExecWB( intOLEcmd, intOLEparam )
{
	// Create OLE Object
	var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 
CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
	// Place Object on page
	document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
	// if intOLEparam is not defined, set it
	if ( ( ! intOLEparam ) || ( intOLEparam < -1 )  || ( 
intOLEparam > 1 ) )
		intOLEparam = 1;
	// Execute Object
	WebBrowser1.ExecWB( intOLEcmd, intOLEparam );
	// Destroy Object
	WebBrowser1.outerHTML = "";
}
// eof
====================================================
There are two ways, the first is obvious, use the print button on the 
browser. However, this is not always possible, say in a popup without a 
tool bar so try: 
 <SCRIPT LANGUAGE="JavaScript"><!--
 if (window.print)
     document.write('<FORM><INPUT TYPE="BUTTON" VALUE="Print" 
onClick="window.print()"><\/FORM>');
 //--></SCRIPT>
This should work but I am of the impression that early IE does not 
support the window.print() method. I dont know how true this is though 
nor when this changes. 
This code is untested, because I refuse to destroy a rainforest testing 
it.



© 1999-2004 Synop Pty Ltd