Printing a document
Another question I see asked a lot on the forums is I want to print a document without loading it. The problem is you have to load the document. The way to hide it from the user is to use a hidden iframe which you load the document.
<iframe id="iframePDF" src="about:blank" style="display:none"></iframe>
<input type="button" value="Print Word" name="btnPrint1" onclick="PrintFrame('test.doc')"/>
<input type="button" value="Print Excel" name="btnPrint2" onclick="PrintFrame('asdf.CSV')"/>
<input type="button" value="Print PDF" name="btnPrint3" onclick="PrintFrame('test.pdf')"/>
<span id="spanMess" style="display:none;color:red;"><h3>Preparing Document For Print</h3></span>
<script type="text/javascript">
function PrintFrame(xFile){
parent.iframePDF.location.href = xFile;
document.getElementById("spanMess").style.display="block";
parent.iframePDF.onload = new function(){
setTimeout("parent.iframePDF.print();
parent.document.getElementById('spanMess').style.display='none';",5000);
}
}
</script>
Now there is one problem with this method. I have to use a timeout to delay the printing. The reason for this is that the onload event fires as soon as the document starts loading, it does not wait until it is completed like a HTML page. Therfore you need to add the timeout. If you document is large, you need to increase the time.
Hope that helps.
Eric Pascarello
Moderator of HTML/JavaScript at www.JavaRanch.com
Author of: JavaScript: Your Visual Blueprint for Dynamic Web Pages
TrackBacks[0]
Comments[5]
Posted by pascarello on May 12, 2005 1:37:49 PM EST
Was my pop up blocked?
I have seen a lot of questions lately about detecting if your pop up window was blocked by a pop up blocker. It is rather simple as shown below:
var winPop = window.open(...) ;
if (winPop==null || typeof(winPop)=="undefined") alert("window blocked");
Hope that helps
Eric Pascarello
Moderator of HTML/JavaScript at www.JavaRanch.com
Author of: JavaScript: Your Visual Blueprint for Dynamic Web Pages
TrackBacks[0]
Comments[0]
Posted by pascarello on May 12, 2005 9:19:41 AM EST