[Javascript] Controling a SELECT from a child window

Nick Fitzsimons nick at nickfitz.co.uk
Wed Sep 21 09:36:19 CDT 2005


> It seems that you are allowed to manipulate objects in other documents (as
> i would expect), but when trying to insert the Option object there is
> something blocking it.
> I think this might be a bug, as it´s not logical behaviour as far as i can
> see.
>

No, it's not a bug, and it is logical once you examine the way the DOM
(Document Object Model) works. When you try to add an option, what you are
actually doing, at DOM level, is:

1. Create a Node of type Element having the tagName "option";
2. Append it as a child of the Node of type Element having the tagName
"select".

The DOM specification states:
a) "The Node objects... have a ownerDocument attribute which associates
them with the Document within whose context they were created." [1]

b) (DomException types:) "WRONG_DOCUMENT_ERR If a node is used in a
different document than the one that created it" [2]

c) (appendChild method:) "WRONG_DOCUMENT_ERR: Raised if newChild was
created from a different document than the one that created this node."
[3]

If you have a script running in a different window to the <select> you are
trying to manipulate, then by definition it is using a different document
to create the <option> element, and therefore the option cannot be added
to the document containing the <select>. If one uses

window.opener.document.createElement("option")

then the <option> element will have been created by the document
containing the <select> to which it is to be appended, and it should work
smoothly. (Internet Explorer bugs might ruin this, but I'm sure I've had
this working in the past.)

HTH,

Nick.

[1]
http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-core.html#i-Document
[2]
http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-core.html#ID-17189187
[3]
http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-core.html#ID-1950641247

-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/



More information about the Javascript mailing list