Donius wrote:
I am making some popup windows, and then trying to load content into
them. It can be as few as two windows, and as many as 15 or so. I'm
running into a problem where firefox cannot find the
popupwindow.document.body after it loads. If i wait until the popups
are all popped, and *then* try to write into them, the script can find
more windows...but still can't get them all to find the document body
element. It gives me the error "winbody has no properties" when i do
something like this:
<code>
popupwin = window.open( '', 'window_name', '' );
winbody = popupwin.document.body;
winbody.appendChild(popupwin.document.createTextNo de('sometext'));
</code>
So. If that made sense, what i would like is anyone who has an idea
for how i can make the script wait until the browser understands the
DOM before executing more scripts, because i need the win.document.body
to *have* properties, and accept the function appendChild and other
such functions. Any ideas?
First of all, Firefox does not have the document.body element, instead refer
to it with window.document.documentElement.
If you still have problems then there are a couple different methods you can
use, but obviously you will have to decide which one works best for your
situation. First of all you could put an onload event into the window you
are opening that fires a method in the opener window. Here's a little code
snippet:
popupwin = window.open(
'<HTML><BODY onload = "window.opener.myContFunction()"></BODY></HTML>',
'window_name', '' );
function myContFunction()
{
winbody = popupwin.document.documentElement;
winbody.appendChild(popupwin.document.createTextNo de('sometext'));
}
If you do that you shouldn't ever have any problems with getting that error
anymore. Another method of achieving things is a little more "squirrely"
and probably not too nice of a coding technique, but it should work. It
uses setInterval to check (every 10 milliseconds in the example below) if
the popupwin.document.body exists yet. Here is an example:
popupwin = window.open(
'<HTML><BODY onload = "window.opener.myContFunction()"></BODY></HTML>',
'window_name', '' );
myInterval = window.setInterval("myContFunction()",10)
function myContFunction()
{
if (popupwin && popupwin.document && popupwin.document.documentElement)
{
cancelInterval(myInterval)
winbody = popupwin.document.documentElement;
winbody.appendChild(popupwin.document.createTextNo de('sometext'));
}
}
Hope you find this helpful!
David