I'm trying to identify objects in javascript. My problem is, that msie DOM nodes are not instances of any Object descendant, so I cannot set or get property on such an instance. I'm trying to make a workaround for that with htc behaviors (only nodes with style can have behavior, so this is a half solution, but better than nothing):
identity.htc:
<PUBLIC:COMPONENT>
<script type="text/javascript">
for(property in Object.prototype)
{
element[property]=Object.prototype[property];
}
</script>
</PUBLIC:COMPONENT>
test.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>javascript identity workaround</title>
<!--[if IE]>
<style type="text/css">
*{behavior:url(identity.htc)}
</style>
<![endif]-->
<script type="text/javascript">
(function ()
{
var i=0;
var table={};
Object.prototype.identity=function ()
{
if (!this.__hashcode__)
{
this.__hashcode__=++i;
table[i]=this;
}
return this.__hashcode__;
};
Object.identify=function (i)
{
return table[i];
};
})();
window.onload=function ()
{
var body=document.getElementsByTagName("body")[0];
var existingElement=document.getElementById("existingElement");
var newElement=document.createElement("div");
newElement.addBehavior("identity.htc");
alert(body.identity()); //1
alert(existingElement.identity()); //2
alert(newElement.identity()); // expected: 3, real: method not supported
};
</script>
</head>
<body>
<div id="existingElement"></div>
</body>
</html>
My problem is, that I cannot use the identity method on the newly created elements. I tried to add the behavior with the addBehavior method, but it didn't help. Has somebody a solution for this?
I got the solution: