Strange behavior with Javascript's __defineSetter__
Posted
by
Shea Barton
on Stack Overflow
See other posts from Stack Overflow
or by Shea Barton
Published on 2011-01-07T04:16:54Z
Indexed on
2011/01/07
10:53 UTC
Read the original article
Hit count: 264
I have a large project in which I need to intercept assignments to things like element.src, element.href, element.style, etc. I figured out to do this with defineSetter, but it is behaving very strangely (using Chrome 8.0.552.231)
An example:
var attribs = ["href", "src", "background", "action", "onblur", "style", "onchange", "onclick", "ondblclick", "onerror", "onfocus", "onkeydown", "onkeypress", "onkeyup", "onmousedown", "onmousemove", "onmouseover", "onmouseup", "onresize", "onselect", "onunload"];
for(a = 0; a < attribs.length; a++) {
var attrib_name = attribs[a];
var func = new Function("attrib_value", "this.setAttribute(\"" + attrib_name + "\", attrib_value.toUpperCase());");
HTMLElement.prototype.__defineSetter__(attrib_name, func);
}
What this code should do is whenever common element attribute in attribs is assigned, it uses setAttribute() to set a uppercased version of that attribute.
For some very strange reason, the setter works for only ~1/3 of the assignments.
For example with
element.src = "test"
the new src is "TEST", like it should be
however with
element.href = "test"
the new href is "test", not uppercase
then even when I try element.__lookupSetter__("href")
, it returns the proper, uppercasing setter
the strangest thing is different variables are intercepted properly between Chrome and Firefox
help!!
© Stack Overflow or respective owner