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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

innerHTML problem

Status
Not open for further replies.

Idee

Programmer
Jul 8, 2003
51
NZ
Hi all

A page is built programatically and the logic to built and to display the page has been put into <span> tag. On clicking a button, it should open a new window with all the information within <span tags.

This works some times and some times, it just opens a blank window.

<script language="javascript">
function PrintFriendly(){
a = window.open('', 'mycal', 'height=350,width=700,menubar=1,resizable=1,scrollbars=1,top=0,left=0');
a.document.body.innerHTML = Printpage.innerHTML;
}
</script>

<span id="Printpage">
----- Logic and display for the page -----
</span>
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td colspan="7" align="center">
<button onclick="PrintFriendly()">Print </button>
</td>
</tr>
</table>


Any ideas are much appreciated?

Thanks
 
Two ideas:

- contents of Printpage is not always OK and visible when "pasted" into opened window
- window takes some time to open

To check last, use setTimeout() to assign innerHTML. Or better, initiate the same code from popup window (body onload event).
 

I'm with Vongrunt on this one. Using the onload event of the popup body is the best bet. I'd do this by loading a blank template page containing only a callback function into the popup... Something like this:

Code:
<html>
<head></head>
<body onload="window.opener.document.popupLoaded();"></body>
</html>

And then have a "popupLoaded()" function on your page with the popup opening code. Inside this, you can guarantee that the popup has finished loading. Oh - and you'd need to load that template as the first parameter to your window.open command instead of using ''.

Hope this helps,
Dan
 
Thanks to both of you for your suggestions.

I have a problem here, the whole page template is written somewhere else in the code and I can not control that as that is common template for all the pages in the site.

I have control over this page which doen't have body tag.

Can you pl suggest some other way of handling it?

Many Thanks
 
Can you at least specify URL for window.open() instead of empty string ('')?
 
Do you think that will make a difference? As it is the same page but the content to be written in the popup window is the part within <span> tag. It works fine most of the time but doesn't work sometimes.

Thanks
 
Depending on true reason why popup goes blank from time to time - yes. That way you can use body onload event for callback or assigning. Here is sample code:
Code:
<script language="javascript">
function PrintFriendly(){
  a = window.open('blah.htm', 'mycal', 'height=350,width=700,menubar=1,resizable=1,scrollbars=1,top=0,left=0');
}
</script>
... and file blah.htm:
Code:
<html>
<head></head>
<body onload="document.body.innerHTML = window.opener.document.getElementById('Printpage').innerHTML;"></body>
</html>
This assumes that Printpage always exists in opener window.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top