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

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

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

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 2nd, 2009 06:35
engatoo engatoo, Rockys rainwal, Raj Aryan, Thomas Loo, Martin Honnen, exan wara, http://www.wildlifeindiatravel.com/


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>
http://www.jaipurtravelguide.com/
http://www.jaipurtravels.com/
http://www.jaipurjaipur.com/
http://www.jaipurtourismguide.com/
http://www.rajasthantravelsguide.com/
http://www.rajasthantravelguide.co.in/
http://www.travelpackagerajasthan.com/
http://www.namaskarindiatravels.com/
http://www.indianhotelsindia.com/
http://www.travelpackageindia.com/
http://www.navyatravels.com/
http://www.hillstationindiatour.com/
http://www.wildlifeindiatravel.com/ 
http://www.pushkartravels.com/
http://www.udaipurtravels.com/
http://www.agratravels.com/
http://indiantravelguide.co.in/
http://www.traveltoindia.biz/
http://www.info4india.com/
http://www.indiantravelsguide.com/
http://www.indiantravelguideindia.com/
http://rajasthantravelguide.wordpress.com/
http://attractioninjaipur.blogspot.com/
http://jaipurtravelsguide.blogspot.com/
http://hoteles-en-la-india.blogspot.com/
http://jaipurguiadeviajes.blogspot.com/
http://jaipurrajasthanindia.blogspot.com/
http://travelguiderajasthan.blogspot.com/
http://travelguiderajasthan.blogspot.com/
http://wildlifeindiatravel.blogspot.com/
http://indiantravelguideindia.blogspot.com/
http://indianhotelsindia.blogspot.com/
http://rajasthantravelsguide.blogspot.com/
http://pushkartravels.blogspot.com/
http://indiatravelsguide.blogspot.com/
http://goaguidedevoyage.blogspot.com/
http://goatravelsguide.blogspot.com/
http://indiaguadeviajes.blogspot.com/
http://htelseninde.blogspot.com/
http://indienguidedevoyage.blogspot.com/
http://indianhillstationsindiatravel.blogspot.com/
http://jaipurtravelguiderajasthan.blogspot.com/
http://hillstationinindia.blogspot.com/
http://jaipurcarrentals.blogspot.com/
http://indiabudgettourtravelpackage.blogspot.com/
http://romanticrajasthanhoneymoontour.blogspot.com/
http://indianadventuretourpackages.blogspot.com/
http://rajasthantraintourpackage.blogspot.com/
http://jaipurinformationguide.blogspot.com/
http://rajasthanculturaltourpackages.blogspot.com/
http://uttaranchaltourpackages.blogspot.com/
http://luxurytourspackages.blogspot.com/
http://indiahimalayatourtravelpackages.blogspot.com/
http://eastindiatravelpackages.blogspot.com/
http://indiahillstationstravelpackages.blogspot.com/
http://indiatravelpackages.blogspot.com/
http://westindiatourpackages.blogspot.com/
http://lehladakhtourpackages.blogspot.com/
http://indiabudgettourtravelpackage.blogspot.com/
http://romanticrajasthanhoneymoontour.blogspot.com/
http://worldtravelsguide.blogspot.com/
http://jaipursilverjewellery.com/
http://www.jaipurhandicrafts.com/
http://www.roo7e.com
http://www.roo7e.com
http://www.al33ab.com
http://ksacam.com
http://chat.ksacam.com
http://ksacam.info
http://oo2o.com
http://s4cam.com
http://saudi4cam.com				
http://voice.ksacam.com
http://سعودي-كام.oo2o.com
http://سعوديكام.com
http://xn--mgbply5cnrr.com
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/
http://www.dir.3rabstarz.com/
http://www.up.3rabstarz.com
http://www.3rabstarz.com/vb/forumdisplay.php?f=5
http://www.3rabstarz.com/vb/forumdisplay.php?f=116
http://www.3rabstarz.com/vb/forumdisplay.php?f=66
http://www.3rabstarz.com/vb/forumdisplay.php?f=78
http://www.3rabstarz.com/vb/forumdisplay.php?f=147
http://www.3rabstarz.com/vb/forumdisplay.php?f=2
http://www.3rabstarz.com/vb/forumdisplay.php?f=31
http://www.3rabstarz.com/vb/forumdisplay.php?f=136
http://www.3rabstarz.com/vb/forumdisplay.php?f=34
http://www.3rabstarz.com/vb/forumdisplay.php?f=89
http://www.3rabstarz.com/vb/forumdisplay.php?f=26
http://www.3rabstarz.com/vb/forumdisplay.php?f=72
http://www.3rabstarz.com/vb/forumdisplay.php?f=73
http://www.3rabstarz.com/vb/forumdisplay.php?f=149
http://www.3rabstarz.com/vb/forumdisplay.php?f=150
http://www.3rabstarz.com/vb/forumdisplay.php?f=151
http://www.3rabstarz.com/vb/forumdisplay.php?f=152
http://www.3rabstarz.com/vb/forumdisplay.php?f=67
http://www.3rabstarz.com/vb/forumdisplay.php?f=3
http://www.3rabstarz.com/vb/forumdisplay.php?f=37
http://www.3rabstarz.com/vb/forumdisplay.php?f=115
http://www.3rabstarz.com/vb/forumdisplay.php?f=76
http://www.3rabstarz.com/vb/forumdisplay.php?f=77
http://www.3rabstarz.com/vb/forumdisplay.php?f=36
http://www.3rabstarz.com/vb/forumdisplay.php?f=153
http://www.3rabstarz.com/vb/forumdisplay.php?f=122
http://www.3rabstarz.com/vb/forumdisplay.php?f=118
http://www.3rabstarz.com/vb/forumdisplay.php?f=119
http://www.3rabstarz.com/vb/forumdisplay.php?f=120
http://www.3rabstarz.com/vb/forumdisplay.php?f=121
http://www.3rabstarz.com/vb/forumdisplay.php?f=35
http://www.3rabstarz.com/vb/forumdisplay.php?f=65
http://www.3rabstarz.com/vb/forumdisplay.php?f=7
http://www.3rabstarz.com/vb/forumdisplay.php?f=9
http://www.3rabstarz.com/vb/forumdisplay.php?f=11
http://www.3rabstarz.com/vb/forumdisplay.php?f=142
http://www.3rabstarz.com/vb/forumdisplay.php?f=143
http://www.3rabstarz.com/vb/forumdisplay.php?f=146
http://www.3rabstarz.com/vb/forumdisplay.php?f=140
http://www.3rabstarz.com/vb/forumdisplay.php?f=137
http://www.3rabstarz.com/vb/forumdisplay.php?f=114
http://www.3rabstarz.com/vb/forumdisplay.php?f=117
http://www.3rabstarz.com/vb/forumdisplay.php?f=113
http://www.3rabstarz.com/vb/forumdisplay.php?f=51
http://www.3rabstarz.com/vb/forumdisplay.php?f=84
http://www.3rabstarz.com/vb/forumdisplay.php?f=138
http://www.3rabstarz.com/vb/forumdisplay.php?f=139
http://www.3rabstarz.com/vb/forumdisplay.php?f=15
http://www.3rabstarz.com/vb/forumdisplay.php?f=130
http://www.3rabstarz.com/vb/forumdisplay.php?f=97
http://www.3rabstarz.com/vb/forumdisplay.php?f=102
http://www.3rabstarz.com/vb/forumdisplay.php?f=101
http://www.3rabstarz.com/vb/forumdisplay.php?f=104
http://www.3rabstarz.com/vb/forumdisplay.php?f=131
http://www.3rabstarz.com/vb/forumdisplay.php?f=103
http://www.3rabstarz.com/vb/forumdisplay.php?f=99
http://www.3rabstarz.com/vb/forumdisplay.php?f=59
http://www.3rabstarz.com/vb/forumdisplay.php?f=60
http://www.3rabstarz.com/vb/forumdisplay.php?f=39
http://www.3rabstarz.com/vb/forumdisplay.php?f=40
http://www.3rabstarz.com/vb/forumdisplay.php?f=42
http://www.3rabstarz.com/vb/forumdisplay.php?f=4
http://www.3rabstarz.com/vb/forumdisplay.php?f=1
http://www.3rabstarz.com/vb/forumdisplay.php?f=88
http://www.3rabstarz.com/vb/forumdisplay.php?f=148
http://www.3rabstarz.com/vb/forumdisplay.php?f=62
http://www.3rabstarz.com/vb/forumdisplay.php?f=63
http://www.3rabstarz.com/vb/forumdisplay.php?f=64
http://www.3rabstarz.com/vb/forumdisplay.php?f=6
http://www.3rabstarz.com/vb/forumdisplay.php?f=12
http://www.3rabstarz.com/vb/forumdisplay.php?f=96
http://www.3rabstarz.com/vb/forumdisplay.php?f=58
http://www.3rabstarz.com/vb/forumdisplay.php?f=38