Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load Iframe then print

Status
Not open for further replies.

waytech2003

Programmer
Jul 14, 2003
316
US
I have a problem with printing an Iframe after I load it from code. I want to have the user click on a link or button, to load a page into an Iframe. After it has loaded, I want it to print.

The experimental code below will envoke the print feature, but the page has not loaded yet. After you print or cancel, the the page is loaded.


I first tried this without the setTimout() and there was no differance.

How can I print only after the Iframe page is loaded?

Code:
<html>
<head>
<script language="javascript" type="text/javascript"> 

function frameLoad(whichFrame){
	document.getElementById(whichFrame).src="page1.htm";
	window.setTimeout(PrintFrame(whichFrame),5000);
}

function PrintFrame(whichFrame){
	parent[whichFrame].focus();
	parent[whichFrame].print();
}
	
</script>
</head>
<body>
<p onclick="frameLoad('Iframe1');">Load and Print Iframe</p>

<iframe id="Iframe1"></iframe>

</body>
</html>
 
function PrintFrame(whichFrame){
parent[whichFrame].focus();
parent[whichFrame].print();
}
you answered your own question just put that function in page1.htm!
 
Thank you for your reply.

In my website I will have 57 differant pages that get loaded into the Iframe at differant times. So I would have to put this "PrintFrame" function in all pages I guess.

I removed the function from the main page and put it in page1.htm
I now get an error. I assume it is because when I call the fucntion that is no in page1.htm, it has not loaded yet.



 
Not sure if it will work, but could you perhaps use the onLoad event of the frame and assign the printing function to it?

Code:
function frameLoad(whichFrame){
window.frames.whichframe.OnLoad=PrintFrame(whichFrame);   
document.getElementById(whichFrame).src="page1.htm";
}



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I just tried the following and I still get the same results. The print dialog pops up, but the Iframe is still not loaded.

Code:
<html>
<head>
<script language="javascript" type="text/javascript"> 

function frameLoad(whichFrame){
	whichFrame.onload = PrintFrame(whichFrame);
	document.getElementById(whichFrame).src="page1.htm";
}

function PrintFrame(whichFrame){
	parent[whichFrame].focus();
	parent[whichFrame].print();
}
</script>
</head>
<body>
<p onclick="frameLoad('Iframe1');">Load and Print Iframe</p>

<iframe id="Iframe1"></iframe>

</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top