...

View Full Version : Print DIV in iframe



buchananj
03-25-2005, 03:50 PM
My goal is to have a page with an iframe on it. The page in the iframe will have a div. I need a button or link on the main page that will print the contents of the div within the iframe. I know how to print the contents of an iframe from the main page using the code below. I also know how to print the contents of a div on the main page with the code below. I just don't know how to combine the two to print the div in the iframe. Thank you in advance to anyone who can help.

Here is my code to print out the contents of iframe (I1):

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

function PrintThisPage()
{

if (CheckIsIE() == true)
{
document.I1.focus();
document.I1.print();
}
else
{
window.frames['I1'].focus();
window.frames['I1'].print();
}
}
</script>
And here is my code to print contents of a div on the same page as the button or link:

<script language="JavaScript">
var gAutoPrint = true; // Flag for whether or not to automatically call the print function

function printSpecial(name)
{
var name
if (document.getElementById != null)
{
var html = '<HTML>\n<HEAD>\n';

if (document.getElementsByTagName != null)
{
var headTags = document.getElementsByTagName("head");
if (headTags.length > 0)
html += headTags[0].innerHTML;
}

html += '\n</HE' + 'AD>\n<BODY>\n';

var printReadyElem = document.getElementById(name);

if (printReadyElem != null)
{
html += printReadyElem.innerHTML;
}
else
{
alert("Could not find the printReady section in the HTML");
return;
}

html += '\n</BO' + 'DY>\n</HT' + 'ML>';

var printWin = window.open("",name);
printWin.document.open();
printWin.document.write(html);
printWin.document.close();
if (gAutoPrint)
printWin.print();
}
else
{
alert("Sorry, the print ready feature is only available in modern browsers.");
}
}
</script>

ecline
02-21-2008, 12:03 AM
Tested in IE7


<head>
<script type="text/javascript">
function ClickHereToPrint(){
try{
var oIframe = document.getElementById('ifrmPrint');
var oContent = document.getElementById('divToPrint').innerHTML;
var oDoc = (oIframe.contentWindow || oIframe.contentDocument);
if (oDoc.document) oDoc = oDoc.document;
oDoc.write("<html><head><title>title</title>");
oDoc.write("</head><body onload='this.focus(); this.print();'>");
oDoc.write(oContent + "</body></html>");
oDoc.close();
}
catch(e){
self.print();
}
}
</script>
</head>


<body>

<a onclick="ClickHereToPrint();">Print</a>

<iframe id='ifrmPrint' src='#' style="width:0px; height:0px;"></iframe>
<div id="divToPrint">
content
</div>
</body>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum