Javascript  -  Browser Print Button

Printing a page in a web browser - more specifically, setting up a button so that the viewer can click the page and have it print automatically - has been a bit of a holy grail for web designers workin with forms and information which may need to be available in ‘hardcopy’ for their customers.

To the left is a sample button which. when clicked, should trigger a 4.x level browser’s “Print” function.

Here’s a script, found by Team NetObjects member David Lewis on http://www.dynamicdrive.com (a very nice site for DHTML and JavaScript)

In the “Layout HTML” for the page you wish to set this up on, insert this code:

<script language = "JavaScript">
/* This script is written by Eric (webcrawl@usa.net)
For full source code, installation instructions,
100's more JavaScripts and Terms of Use, visit
dynamicdrive.com */
function printit() {
   if (navigator.appName == "Netscape") {
       window.print();
   } else {
       try {
           var webBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"> </OBJECT>';
           document.body.insertAdjacentHTML('beforeEnd', webBrowser);
           WebBrowser1.ExecWB(6,2);
           //Use a 1 vs. a 2 for a prompting dialog box
           WebBrowser1.outerHTML = "";
       } catch(e) {
           window.location.reload();
           // Or the button will never work again!
           // This fixes a bug in IE5.x
       }
   }
}
</script>



Next, create a small layout area

Check the “Layout Area is a Form” checkbox

Place a Button in that layout area, and set it to type “Button” in the “Form Button Properties” window.

Right-Click (Control-Click if you’re on a Mac) on the Button and select “Object HTML” from the Pop-Up menu that appears

In the “Inside Tag” field, enter this code:

onClick=”printit()”



... and that should do it! One caveat - IE5 may not work consistantly with this function, particularly if you click it, then cancel the “Pirint” window, then click it again.

Go Back to Previous Page

Have a question that you can´t find the answer to? Want to report a possible bug?

Send a help request to NetObjects Technical Support