Print contents of IFRAME from parent window

Here's a quick little tip that demonstrates how to print the contents of an IFRAME from the parent window using JavaScript.

Here's a quick little tip that demonstrates how to print the contents of an IFRAME from the parent window using JavaScript. I've seen on several discussion forums that this isn't possible when it is. The key item to notice is that we set focus() to the IFRAME just before issuing the print() method.

Please take a moment to rate this tip: http://www.turnkeytools.com/polls/default.asp?POLLID=10000086&DOMAINID=4610


<script language=JavaScript>

function CheckIsIE()
{
if (navigator.appName.toUpperCase() == 'MICROSOFT
INTERNET EXPLORER') { return true;}
else { return false; }
}


function PrintThisPage()
{

if (CheckIsIE() == true)
{
document.ifWorkspace.focus();
document.ifWorkspace.print();
}
else
{
window.frames['ifWorkspace'].focus();
window.frames['ifWorkspace'].print();
}

}

</script>

In the parent window, just put a link or button to call the PrintThisPage() method:

<a href="javascript:PrintThisPage();" >Print This Page</a>


Submission Date:  9/23/2005 3:00:06 PM
Submitted By:  Robbe Morris
My Home Page:  http://www.robbemorris.com

By Robbe Morris   Popularity  (1496 Views)
Picture
Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of NullSkull.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.  Robbe also loves to scuba dive and go deep sea fishing in the Florida Keys or off the coast of Daytona Beach. Microsoft MVP