Printing an Iframe



Give the iframe a name and use

window['iframe_name'].focus()
window['iframe_name'].print()


Printing part of a page

You could also use an iframe to print parts of a page

The basic idea is that because the print command prints the whole page you populate a new page containing only the text you want to print and print that page.
An hidden iframe is loaded with a page that will be dynamically populated with information from the parent page, the iframe page is then printed.

This is some example text the will be transferred to the iframe page for printing.
Print the above


The contents of this div will be transferred for printing.

Example table And an image

Print the above

Main Page

<script type="text/javascript">
function printMe(id){
window.frames["printeriframe"].document.getElementById("printcontent").innerHTML = document.getElementById(id).innerHTML
window.frames['printeriframe'].printMe()
} </script> <iframe name="printeriframe" id="printeriframe" src="iframepage.htm" style="position:absolute;left:0;top:-1000px"></iframe> <div id="d1">Hello World</div> <a href="#null" onclick="printMe('d1')">Print</a> <div id="d2">Dummy Text</div> <a href="#null" onclick="printMe('d2')">Print</a>

Iframe Page

<script type="text/javascript">
function printMe(){
self.focus()
self.print()
}
</script>

<div id="printcontent">d1</div>


Print