Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q49 Can I print the current page?

irt.org | Knowledge Base | JavaScript | Print | Q49 [ previous next ]

Q49 Can I print the current page?

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?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 6th July 2009. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2009 irt.org, All Rights Reserved.