You are here: irt.org | FAQ | JavaScript | Print | Q481 [ previous next ]
Ctrl-P will invoke the print dialogue.
In JavaScript the print() function works in Netscape Navigator 4+ and Internet Explorer 5 beta 2+.
Someone (I forget who) sent me the following VBScript/ActiveX code that is supposed to work in Internet Explorer 3 and Internet Explorer 4:
<script language="JavaScript"><!--
DA = (document.all) ? 1 : 0
//window.onerror=handle_error
function handle_error()
{
alert("\nNothing was printed. \n\nIf you do want to print this page, then\nclick on the printer icon in the toolbar above.")
// to cancel the script error warning
return true;
}
//--></script>
<form>
<input type="button" value="Print Page" onClick="window.print();">
</form>
<script language="VBScript">
sub window_onunload
on error resume next
' Just tidy up when we leave to be sure we aren't
' keeping instances of the web-browser control in memory
set WB = nothing
end sub
sub print
OLECMDID_PRINT = 6
OLECMDEXECOPT_DONTPROMPTUSER = 2
OLECMDEXECOPT_PROMPTUSER = 1
on error resume next
'Internet Explorer 4 object has different command structure
if DA then
call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER)
else
call WB.IOleCommandTarget.Exec(OLECMDID_PRINT ,OLECMDEXECOPT_DONTPROMPTUSER,"","")
end if
if err.number <> 0 then
if DA then ' Internet Explorer 4 they probably cancelled
alert "Nothing Printed :" & err.number & " : " & err.description
else
handle_error ' ie3x give alternate instructions
end if
end if
end sub
'This will be interpreted during loading.
'It will write out the correct webbrowser object depending
'on the browser version. To be sure it works, make sure you
'include this script block in between the body tags.
if DA then
'this must be Internet Explorer 4 or greater
wbvers="8856F961-340A-11D0-A96B-00C04FD705A2"
else
'this must be Internet Explorer 3.x
wbvers="EAB22AC3-30C1-11CF-A7EB-0000C05BAE0B"
end if
document.write "<OBJECT ID=""WB"" WIDTH=0 HEIGHT=0 CLASSID=""CLSID:"
document.write wbvers & """> </OBJECT>"
</script>