Entry
(How) can I print a document that is not currently loaded into a frame or window?
(How) can I print a document that is not currently loaded into a frame or window?
2nd answer can't use on a frame base application !
Apr 24th, 2002 19:01
Thomas Loo, Martin Honnen, exan wara,
The answer is you cannot print a document not loaded in any frame or
window. However with NN6 and with IE5+ you can use an invisible iframe
to load the page and then print it. With NN4 you need to open a new
window to load the page into it and then print it.
The following contains the code of a printURL function implementing
the above described concepts. Note that hiding an iframe but
nevertheless use it to load and print documents is somehow
problematic: the easiest way seems to be to use CSS display: none; but
NN6 (at least current versions 6.0-6.2) don't load the frame at all if
display is set to none. With IE you have the problem that you need to
set focus to an iframe to ensure it is printed and IE throws an error
if you call focus on an invisible element. Therefore the CSS below
keeps the iframe absolutely positioned with width and height set to
0px.
I tested the code to work with NN6.2 and IE6 on Win. Both allow to
print html pages or images loaded from the same server as the page is
coming from but do not allow to print page from other servers. I
believe the code should work the same with IE5 and IE5.5. As for
earlier NN6 version try it, there have been bugs with onload handlers
but I think at least NN6.1 should work too.
With NN4 I tested that the code is able to print both pages from the
same server as well as from another server.
<html>
<head>
<title>
printing another page
</title>
<style type="text/css">
#printerIframe {
position: absolute;
width: 0px; height: 0px;
border-style: none;
/* visibility: hidden; */
}
</style>
<script type="text/javascript">
function printURL (url) {
if (window.print && window.frames && window.frames.printerIframe) {
var html = '';
html += '<html>';
html +=
'<body onload="parent.printFrame(window.frames.urlToPrint);">';
html += '<iframe name="urlToPrint" src="' + url + '"><\/iframe>';
html += '<\/body><\/html>';
var ifd = window.frames.printerIframe.document;
ifd.open();
ifd.write(html);
ifd.close();
}
else {
if (confirm('To print a page with this browser ' +
'we need to open a window with the page. Do you want to continue?'))
{
var win = window.open('', 'printerWindow',
'width=600,height=300,resizable,scrollbars,toolbar,menubar');
var html = '';
html += '<html>';
html +=
'<frameset rows="100%, *" ' +
'onload="opener.printFrame(window.urlToPrint);">';
html += '<frame name="urlToPrint" src="' + url + '" \/>';
html += '<frame src="about:blank" \/>';
html += '<\/frameset><\/html>';
win.document.open();
win.document.write(html);
win.document.close();
}
}
}
function printFrame (frame) {
if (frame.print) {
frame.focus();
frame.print();
}
}
</script>
</head>
<body>
<form name="gui">
<input type="button" value="print page whatever.html"
onclick="printURL('whatever.html');"
/>
</form>
<a href="kiboInside.gif" target="_blank"
onclick="printURL(this.href); return false"
>print image kiboInside.gif</a>
|
<a href="http://JavaScript.faqts.com" target="_blank"
onclick="printURL(this.href); return false;"
>print http://JavaScript.FAQTs.com</a>
<iframe name="printerIframe" id="printerIframe"
src="about:blank"
></iframe>
</body>
</html>
--------------------------------------------------------------
Actually, with MSIE 5 and 5.5 (don't know about 6.0) there is a way to
print virtually any remote document without first loading/displaying it
in the browser. MSIE provide a LINK element for specifying an alternate
print source instead of the current document and it behaves as any
other DOM element.
The magic element looks like:
<LINK REL="alternate" media="print" href="..."/>
So, to get a little constructive we knock up a function that allow us
to create and manipulate this element dynamically...
<html>
<head>
<title>printing a remote document</title>
<script>
function printURL(sHref) {
if(document.getElementById && document.all && sHref){
if(!self.oPrintElm){
var aHeads = document.getElementsByTagName('HEAD');
if(!aHeads || !aHeads.length)
return false;
if(!self.oPrintElm)
self.oPrintElm = document.createElement('LINK');
self.oPrintElm.rel = 'alternate';
self.oPrintElm.media = 'print';
aHeads[0].appendChild(self.oPrintElm);
}
self.oPrintElm.href = sHref;
self.focus();
self.print();
return true;
}
else return false;
}
</script>
</head>
<body>
<a target="_blank"
href="http://www.saltstorm.net/"
onclick="printURL(this.href); return false;"
>print http://www.saltstorm.net</a>
</body>
</html>