By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,729 Members | 1,487 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,729 IT Pros & Developers. It's quick & easy.

Firefox not loading dom quickly enough: how to wait?

P: n/a
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?

Aug 9 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Aug 10 '05 #2

P: n/a
On Tue, 9 Aug 2005 19:58:30 -0700, "David" <dk****@hotmail.com> wrote:
Donius wrote:
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.


It doesn't? Are you sure? Why would it choose to ignore standards
for that?

http://www.w3.org/TR/2003/REC-DOM-Le...ml#ID-26809268

Now it's true of course that documentElement would also exist, but
that doesn't mean document.body doesn't.

Jim.
Aug 10 '05 #3

P: n/a
Thanks, both!

(1) Yes, document.body does indeed exist in firefox's JS engine.
(2) The syntax for window.open takes the arguments: (file_name,
window_name, other_arguments_csv_style), as far as i can tell, so i
can't specify content like that
(3) I didn't think that document.write'ing an onload event handler
would work. Not sure why, it just was ruled out in my head as an
option. But when you mentioned it, David, i tried it and viola!
Everything i could have ever wanted. :) Thanks so much! So the basic
was like this:

<code>
popupwin = window.open( '', 'window_name', '' );
windoc = popupwin.document;
windoc.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0
Transitional//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<head>\n
</head>\n<body onload="window.opener.myContFunction()">\n</body>");
windoc.close();

function myContFunction(){
winbody = windoc.body;
winbody.appendChild(windoc.createTextNode('sometex t'));
}
</code>

Also the little bit of syntax where you called the function of the
opener...that helped too. I didn't think of scope in that sense...i
just kind of assumed it would all be available to the popup windows as
children of the main. And heck, even if by some craziness they
are...it's cleaner for me to think of the opener as containing all the
useful code. :) Thanks!

-Brendan

Aug 10 '05 #4

P: n/a
Donius wrote:
Thanks, both!

(1) Yes, document.body does indeed exist in firefox's JS engine.
(2) The syntax for window.open takes the arguments: (file_name,
window_name, other_arguments_csv_style), as far as i can tell, so i
can't specify content like that
(3) I didn't think that document.write'ing an onload event handler
would work. Not sure why, it just was ruled out in my head as an
option. But when you mentioned it, David, i tried it and viola!
Everything i could have ever wanted. :) Thanks so much! So the basic
was like this:

<code>
popupwin = window.open( '', 'window_name', '' );
windoc = popupwin.document;
windoc.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0
Transitional//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<head>\n
</head>\n<body onload="window.opener.myContFunction()">\n</body>");
windoc.close();

function myContFunction(){
winbody = windoc.body;
winbody.appendChild(windoc.createTextNode('sometex t'));
}
</code>

Also the little bit of syntax where you called the function of the
opener...that helped too. I didn't think of scope in that sense...i
just kind of assumed it would all be available to the popup windows as
children of the main. And heck, even if by some craziness they
are...it's cleaner for me to think of the opener as containing all the
useful code. :) Thanks!

-Brendan


Sorry for the misinformation about the body object not existing, I don't
know how I'd gotten that into my head. I think it's because I had a problem
a while ago that had to do with the body object and I ended up having to use
documentElement instead, and for some reason I'd gotten into my head that
Firefox didn't support the body object like that.

As for using window.open like I specified, I was a little to hasty writing
my code example, instead it should have been as follows:

popupwin = window.open("javascript:document.write(
'<HTML><BODY onload = "window.opener.myContFunction()"></BODY></HTML>')",
'window_name', '' );

And that will work such that when the window loads it runs the code and thus
writes that HTML into the document. However, with that method, you may have
some issues with trying to access the window.opener from the new window due
to cross-domain rules.

Glad you were able to get things working!

David
Aug 10 '05 #5

P: n/a
David wrote:
Sorry for the misinformation about the body object not existing, I don't
know how I'd gotten that into my head. I think it's because I had a problem
a while ago that had to do with the body object and I ended up having to use
documentElement instead, and for some reason I'd gotten into my head that
Firefox didn't support the body object like that.


Well I can confirm that you might stumble into horrible problems if you
use 'normal' DOM properties and methods and combine them with HTML DOM
properties and methods. In my case it had to do with tables.

If you ever see a pure virtual function call error and Internet Explorer
crashes then rewrite your code and take out HTML DOM script.
Aug 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.