Entry
How to print an image when it is clicked?
Mar 2nd, 2000 16:56
Martin Honnen,
The following code works with NN4+ and IE5 (for IE4 check the knowledge
base entry on how to print a page); it opens a page with the clicked
image where the onload handler calls the window.print function:
<HTML>
<HEAD>
<SCRIPT>
var w;
function printImage (img) {
img = img.src ? img.src : img;
if (w && !w.closed)
w.close();
w = open ('', 'imagePrint',
'menubar=1,locationbar=0,statusbar=0,resizable=1'
+ ',scrollbars=1,width=400,height=400');
var html = '';
html += '<HTML><BODY ONLOAD="if (window.print) window.print(); '
+ 'setTimeout(\'window.close()\', 10000);">';
html += '<IMG SRC="' + img + '">';
html += '<\/BODY><\/HTML>';
w.document.open();
w.document.write(html);
w.document.close();
}
</SCRIPT>
</HEAD>
<BODY>
<H3>Click image to print it</H3>
<A HREF="javascript: void 0"
ONCLICK="printImage(document.image1); return false;"
><IMG NAME="image1" SRC="whatever.gif" BORDER="0"></A>
<BR>
<A HREF="javascript: void 0"
ONCLICK="printImage(document.image2); return false;"
><IMG NAME="image2" SRC="whatelse.gif" BORDER="0"></A>
</BODY>
</HTML>
The printImage function takes either an Image object or a image url as
the argument.