Online Shopping : Computers : Programming : Languages : JavaScript : Document

+ Search
Add Entry AlertManage Folder Edit Entry Add page to
Did You Find This Entry Useful?

77 of 103 people (75%) answered Yes
Recently 8 of 10 people (80%) answered Yes


(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 2nd, 2009 06:35
engatoo engatoo, Rockys rainwal, Raj Aryan, 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 
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.
printing another page
<style type="text/css">
#printerIframe {
  position: absolute;
  width: 0px; height: 0px;
  border-style: none;
  /* visibility: hidden; */
<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;;
  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 ='', 'printerWindow', 
      var html = '';
      html += '<html>';
      html += 
'<frameset rows="100%, *" ' +
      html += '<frame name="urlToPrint" src="' + url + '" \/>';
      html += '<frame src="about:blank" \/>';
      html += '<\/frameset><\/html>';;
function printFrame (frame) {
  if (frame.print) {
<form name="gui">
<input type="button" value="print page whatever.html" 
<a href="kiboInside.gif" target="_blank"
   onclick="printURL(this.href); return false"
>print image kiboInside.gif</a>
<a href="" target="_blank"
   onclick="printURL(this.href); return false;"
<iframe name="printerIframe" id="printerIframe"
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 
in the browser. MSIE provide a LINK element for specifying an 
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...
<title>printing a remote document</title>
function printURL(sHref) {
  if(document.getElementById && document.all && sHref){
      var aHeads = document.getElementsByTagName('HEAD');
      if(!aHeads || !aHeads.length)
        return false;
        self.oPrintElm = document.createElement('LINK');
      self.oPrintElm.rel = 'alternate'; = 'print';
    self.oPrintElm.href = sHref;
    return true;
  else return false;
<a target="_blank"
 onclick="printURL(this.href); return false;"