H -

They have: 62 posts

Joined: Dec 1998

How to Print via Scripting

Netscape Navigator 4 supports printing thru the window method print().
Internet Explorer 4 supports printing thru the web browser object.

Printing via script is not possible in Netscape 3 or Internet Explorer 3.

The cross-browser compatability comes thru a browser Jedi Mind Trick: because a subroutine can be accessed via the
window obect window.functionname, you can name the printing subroutine 'print'. When window.print() is called
on the click of the print button, Netscape will call its built in print method, and Internet Explorer will call the
print routine below. This routine is written in VBScript so Netscape will ignore it.

Note: When Internet Explorer starts to support the print method, this code may need to be modified

An error handler has been added to handle a cancelled print job, a printing error, or a browser that does not support printing.

Here is the sample code for a simple print page:

<html>
<head>
<script language="javascript">
<!--
// IE3 workaround vbscript objects
DA = (document.all) ? 1 : 0

window.onerror=handle_error

function handle_error(){
msg="\nNothing was printed. \n\nTo cancel this print job, click on the printer icon in the toolbar above."
alert(msg);
return true;
}
//-->
</script>
<SCRIPT LANGUAGE="VBScript">
sub print
olecmd = 6 ' Print Command
oleparam = 1

on error resume next

WB.ExecWB olecmd, oleparam

if err.number <> 0 then
if DA then ' IE4 - User Cancelled
alert "Nothing was printed."
else ' IE3 - Other Instructions
handle_error
end if
end if
end sub
</SCRIPT>
</head>

<body>

<P>Sample text Sample text Sample text Sample text Sample text Sample text Sample text Sample text Sample text </P>

<form>
<Input type=button value="print page" onclick="window.print();">
</form>

<OBJECT ID="WB" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>

</BODY>
</HTML>

See the working example at http://www.eons.com/printing.htm

Check out the new printing article on the Microsoft Sitebuilder that shows some alternatives, including a solution for printing frames:

Scripting Support for Web Page Printing

http://www.microsoft.com/workshop/author/script/dhtmlprint.asp

------------------
Jeffrey Ellison
[email protected]
www.eons.com - Free Online Tools for Webmasters

[This message has been edited by Jeffrey Ellison (edited February 07, 1999).]

//edited to try reset date back to 1999//

[This message has been edited by John Pollock (edited 11-20-1999).]

[This message has been edited by John Pollock (edited 11-20-1999).]

John Pollock's picture

He has: 628 posts

Joined: Mar 1999

Maybe this will fix the last post date.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.