Custom properties on DOM nodes

H

Henri Sivonen

I try to associate DOM nodes with other objects. Assigning custom
properties to DOM nodes works in Firefox and Safari. It also works with
HTML nodes in IE6. However, it appears not to work with XML nodes that
are part of trees returned by XMLHttpRequest. How can I work around this
limitation? For XML nodes, I need to be able to associate at most one
object with each node.

The syntax I am using is
node.customproperty = value
 
M

Martin Honnen

Henri said:
I try to associate DOM nodes with other objects. Assigning custom
properties to DOM nodes works in Firefox and Safari. It also works with
HTML nodes in IE6. However, it appears not to work with XML nodes that
are part of trees returned by XMLHttpRequest.

Are you sure about XMLHttpRequest? That is implemented in Mozilla and in
Mozilla there is a common DOM for XML and HTML nodes so I am pretty sure
that at least Mozilla allows you to use an XML DOM node the same way as
an HTML DOM node where you could do
node.someProperty = expression;
The same for Opera 8.00 beta.
I am not sure about Safari and can't test while I am on Windows.
But I rather think IE is the one making problems as there XML DOM is not
integrated in the browser but provided by MSXML as ActiveX objects which
usually do not allow script to add custom properties.
How can I work around this
limitation? For XML nodes, I need to be able to associate at most one
object with each node.

You would need to do it the other way round, create your own wrapper
constructor function e.g.
function MyXmlNode (domNode) {
this.domNode = domNode;
}
then create wrapper objects where needed
var aNode = new MyXmlNode(someDomNode);
and add properties where needed:
aNode.someProperty = expression;
 
H

Henri Sivonen

Martin Honnen said:
Are you sure about XMLHttpRequest? That is implemented in Mozilla and in
Mozilla there is a common DOM for XML and HTML nodes so I am pretty sure
that at least Mozilla allows you to use an XML DOM node the same way as
an HTML DOM node where you could do
node.someProperty = expression;

I formulated my post badly. My point was that it does work on the XML
side in Mozilla and Safari but not in IE.

At least I think it works in Safari. Safari is broken in many ways and I
have not yet fully ported to Safari, but so far this particular feature
appears to work.
The same for Opera 8.00 beta.

Cool. I haven't even tested in Opera 8, yet, because the OS X beta
crashes on launch on my system.
But I rather think IE is the one making problems as there XML DOM is not
integrated in the browser but provided by MSXML as ActiveX objects which
usually do not allow script to add custom properties.
Right.


You would need to do it the other way round, create your own wrapper
constructor function e.g.
function MyXmlNode (domNode) {
this.domNode = domNode;
}
then create wrapper objects where needed
var aNode = new MyXmlNode(someDomNode);
and add properties where needed:
aNode.someProperty = expression;

Thanks. That will get messy, because I will then have to support walking
the tree using the wrappers. :-(
 

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,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top