Custom property disappears after a while in a DOM Node


F

Frycake

Hi all,

Like I explain in the subject, I'm trying to store some information
directly as a property of my DOM nodes.
However after a while the property is dropped.

Context:
* the property store a reference of an element of my array in my
current workspace
* if I add a new one element to the dom node with createElement
function, the custom property doesn't disappear (Seems to).

Any idea ?
 
Ad

Advertisements

T

Thomas 'PointedEars' Lahn

Frycake said:
Like I explain in the subject, I'm trying to store some information
directly as a property of my DOM nodes.

Don't. Those are host objects. Use a native wrapper object instead.
However after a while the property is dropped.
Unlikely.

Context:
* the property store a reference of an element of my array in my
current workspace
* if I add a new one element to the dom node with createElement
function, the custom property doesn't disappear (Seems to).

Well, if it doesn't disappear, everything is fine :)


PointedEars
 
Ad

Advertisements

R

RobG

Hi all,

Like I explain in the subject, I'm trying to store some information
directly as a property of my DOM nodes.
However after a while the property is dropped.

Context:
* the property store a reference of an element of my array in my
current workspace

Presumably you assign a value to a non-standard property of a DOM
element. Is the value a reference to an object or a primitive? Both
seem to "work" in Firefox 3.5.5 and IE 6 (see test case below).

What does "my current workspace" mean? I guess you mean a DOM created
from an HTML document, or are you working in some other environment?

* if I add a new one element to the dom node with createElement
function, the custom property doesn't disappear (Seems to).

Any idea ?

Post a minimal example that exhibits the issue, noting the environment
(e.g. browser and platform) it seems to occur in.

As Thomas said, it's not a good idea to augment host objects. However,
it's good to know if there are specific cases of unexpected behaviour.

Test case:

<div id="div0">Here is a div</div>

<script type="text/javascript">

function readProp(id, prop) {
alert(document.getElementById(id)[prop]);
}

var arr = ['foo','bar'];
var el = document.getElementById('div0');
el.arrRef = arr;

window.setTimeout(function(){
readProp('div0','arrRef');
}, 100);

window.setTimeout(function(){
var el = document.getElementById('div0');
var o = document.createElement('span');
o.appendChild(document.createTextNode(' and a span'));
el.appendChild(o);
}, 100);

window.setTimeout(function(){
readProp('div0','arrRef');
}, 1000);

</script>
 

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

Top