For the impatient, a working example:http://dynom.nl/jquery/print_popup.html
It seems so easy, but I had some trouble printing a popup window containing an image. Whenever I printed the page using the following code it failed.
/**
* FAIL
*/
function printIt() {
var win = window.open('/path/to/image.jpg', 'Image', 'resizable=yes,...');
if (win) {
win.focus();
win.print();
}
return false;
}
So I changed from opening a URL to writing a IMG tag to the opened window, which works like a charm.
/**
* Works like a charm.
*/
function printIt() {
var win = window.open('', 'Image', 'resizable=yes,...');
if (win) {
win.document.writeln('<img src="/path/to/image" alt="image">');
win.document.close();
win.focus();
win.print();
}
return false;
}
And to put it in jQuery terms:
/**
* To put it in jQuery terms:
*/
Popup = {
init : function () {
$('a#action_print').bind('click', Popup.printIt);
},
printIt : function () {
var win = window.open('', 'Image', 'resizable=yes,...');
if (win.document) {
win.document.writeln('<img src="'+ $(this).attr('href') +'" alt="image" />');
win.document.close();
win.focus();
win.print();
}
return false;
}
}
$(document).ready(function () {
Popup.init();
});