Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-25-2005, 02:50 PM   PM User | #1
buchananj
New to the CF scene

 
Join Date: Mar 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
buchananj is an unknown quantity at this point
Print DIV in iframe

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>
buchananj is offline   Reply With Quote
Old 02-20-2008, 11:03 PM   PM User | #2
ecline
New to the CF scene

 
Join Date: Feb 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
ecline is an unknown quantity at this point
Tested in IE7

Code:
<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>
ecline is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:31 PM.


Advertisement
Log in to turn off these ads.