470,811 Members | 1,030 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

DOM nodes as keys to a hash table

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

--
e-mail : cam (at) mcc.id.au icq : 26955922
web : http://mcc.id.au/ msn : cam-msn (at) aka.mcc.id.au
office : +61399055779 jabber : heycam (at) jabber.org
Oct 28 '05 #1
2 2599


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/default.asp?url=/workshop/author/dhtml/reference/properties/uniqueid.asp>
that could help your task for IE HTML DOM scripting.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 28 '05 #2
Martin Honnen wrote:
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/default.asp?url=/workshop/author/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.

--
e-mail : cam (at) mcc.id.au icq : 26955922
web : http://mcc.id.au/ msn : cam-msn (at) aka.mcc.id.au
office : +61399055779 jabber : heycam (at) jabber.org
Oct 29 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Jan-Erik Meyer-Lütgens | last post: by
9 posts views Thread by Rolf Kemper | last post: by
3 posts views Thread by Randell D. | last post: by
90 posts views Thread by Christoph Zwerschke | last post: by
24 posts views Thread by kdotsky | last post: by
3 posts views Thread by Manish | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.