Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q49 Can I print the current page?

You are here: irt.org | FAQ | JavaScript | Print | Q49 [ previous next ]

In JavaScript 1.2 (i.e. Netscape 4.0) there is a new print method that invokes the print dialogue box. Internet Explorer 5 beta 2+ also supports the print method. Before using this method you must test that the browser supports it by testing the window object for a print property:

if (window.print)
    window.print();

The syntax is simply:

windowReference.print()
frameReference.print()

The following will display a print button for JavaScript 1.2 only:

<script language="JavaScript1.2"><!--
document.write('<form><input type="button" value="Print" onClick="window.print()"><\/form>');
//--></script>

Since the above was written Microsoft have released Internet Explorer 4 which supports JavaScript 1.2, but not the window print method. So the following cludge is required to stop the button appearing on Internet Explorer (note it no longer requires the use of JavaScript1.2 - perhaps its not that much of a cludge after all):

<script language="JavaScript"><!--
if (window.print)
    document.write('<form><input type="button" value="Print" onClick="window.print()"><\/form>');
//--></script>

This bug was reported by Andrew Shatwell - thanks Andrew.

Feedback on 'Q49 Can I print the current page?'

©2018 Martin Webb