Give the iframe a name and use
window['iframe_name'].focus()
window['iframe_name'].print()
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.
| Example table | And an image![]() |
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>