PopupWindow.document.getElementById() doesn't work for Mozilla?

N

nick

I have the following code:
var w=window.open('popup.htm','','width=400,height=400');

w.opener = this;

var img = w.document.getElementById('pic');

However, the img variable is always null in Mozilla Firefox. But it works
well in IE.

What's teh proper way to access DOM in the popuped window?
 
M

Michael Winter

I have the following code:
var w=window.open('popup.htm','','width=400,height=400');

w.opener = this;

That is unnecessary. The opener property of the new window will refer to
the opening window automatically.
var img = w.document.getElementById('pic');

However, the img variable is always null in Mozilla Firefox. But it
works well in IE.

Have you named the IMG element (that is, name="pic") or given it an id?
Are you sure that the page has loaded before executing the line above?
What's teh proper way to access DOM in the popuped window?

winRef.document.images['image-name-or-id']

is the best way to access images as it will be supported by both old and
new browsers.

Hope that helps,
Mike
 
D

DU

nick said:
I have the following code:
var w=window.open('popup.htm','','width=400,height=400');

w.opener = this;

Useless and wrong. Avoid coding like that.
var img = w.document.getElementById('pic');

Right here, you're assuming a synchronous chain of event: the window
might be created but the document might still be loading. So you're
referencing the document which has not been loaded yet.
However, the img variable is always null in Mozilla Firefox.

Because the document still has not fired the load event and created
pointers in the memory heap.

But it works
well in IE.

What's teh proper way to access DOM in the popuped window?

Get an onload event in the body of the popup window to call a function
in the opener to access the picture. It works reliably in NS 7.x,
Mozilla 1.x, Firefox 0.8+, K-meleon 0.8+, Opera 7.x, MSIE 6 SP2.

DU
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top