DOM nodes as keys to a hash table

C

Cameron McCormack

Hi everyone.

I want a hash table where DOM nodes are the keys. In Rhino, I can just
use the node objects directly as the keys, since the Java objects that
implement the DOM have handy toString() methods that return a unique
string for each object:

var a = {};
a[document] = ...;
a[document.documentElement] = ...;

This is obviously not portable. The portable solution, I guess, is to
come up with a hash function that works usefully on DOM nodes. Has
anyone thought about this problem and come up with a solution? With a
DOM 3 implementation I could even do something like:

var nodes = [];
var nextIndex = 1;
function getIndex(n) {
var i = n.getUserData("NodeIndex");
if (!i) {
i = nextIndex++;
n.setUserData("NodeIndex", i, null);
}
return i;
}

I want this to work with DOM 2 as well, though.

Any ideas?

Thanks,

Cameron
 
M

Martin Honnen

Cameron McCormack wrote:

var nodes = [];
var nextIndex = 1;
function getIndex(n) {
var i = n.getUserData("NodeIndex");
if (!i) {
i = nextIndex++;
n.setUserData("NodeIndex", i, null);

What is n, a DOM node? At least with HTML DOM nodes in current browsers
I don't know of any problem to simply set a custom JavaScript property
as needed e.g.
if (typeof n.NodeIndex == 'undefined') {
n.NodeIndex = nextIndex++;
}
return n.NodeIndex;

With browsers like Mozilla or Opera which have their own common DOM
implementation for XML and HTML you can certainly do the above for XML
DOM nodes as well but within IE/Win where the XML DOM is implemented by
MSXML that is not possible, trying to set such expando properties throws
an error.

Besides that IE/Win implements a property named uniqueID
<http://msdn.microsoft.com/library/d...uthor/dhtml/reference/properties/uniqueid.asp>
that could help your task for IE HTML DOM scripting.
 
C

Cameron McCormack

Martin said:
What is n, a DOM node? At least with HTML DOM nodes in current browsers
I don't know of any problem to simply set a custom JavaScript property
as needed e.g.
if (typeof n.NodeIndex == 'undefined') {
n.NodeIndex = nextIndex++;
}
return n.NodeIndex;

Yes, but it's not guaranteed to work since the DOM nodes are host objects.
With browsers like Mozilla or Opera which have their own common DOM
implementation for XML and HTML you can certainly do the above for XML
DOM nodes as well but within IE/Win where the XML DOM is implemented by
MSXML that is not possible, trying to set such expando properties throws
an error.

Besides that IE/Win implements a property named uniqueID
<http://msdn.microsoft.com/library/d...uthor/dhtml/reference/properties/uniqueid.asp>
that could help your task for IE HTML DOM scripting.

I'm looking for a general technique that will work in all JS
implementations, but I'll keep your info in mind if I cannot do this.
 

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,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top