Creating currentStyle in Mozilla?

eobet
eobet used Ask the Experts™
on
Hello!

I've seen usage in code on Google searches of something called __defineGetter__ and __defineSetter__, but I have never gotten this to work (HTMLElement.prototype.__defineGetter__("functionName" function... etc. etc.)

However, I did get a response from HTMLElement.prototype.currentStyle = function() { return document.defaultView.getComputedStyle(this, ''); }; but Mozilla only said that my element didn't have any properties when I tried it.

Calling the function explicitly (ie. document.defaultView.getComputedStyle(something.something.something, '').something) DOES however work.

What am I doing wrong in my prototype function?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Finance Systems Developer
Commented:
I'll show you both the __defineGetter and the older syntax to show how they should be. Also, unfortunately, although this works, Mozilla converts colors to RGB values, but apart from that everything else should be fine. Rectangles(for clip) and shorthands may also be problematic. let me know how it works:
<html>
<head><title>Mozilla prototyping</title>
<script>
HTMLElement.prototype.__defineGetter__('currentStyle', function() {
  return (document.defaultView.getComputedStyle(this,""));
})
CSSStyleDeclaration.prototype=function() {
  for (i=0;i<this.length;i++) {
    var prop=this.item(i);
    this[prop]=this.getPropertyValue(prop);
  }
};
</script>
</head>
<body>
<form>
<input type="button" value="Check Style(Red)" onclick="alert(document.getElementById('test').currentStyle.width)">
</form><br>
<div style="color:red" id="test"><i>hello there</i></div>
</body>
</html>
ahosangFinance Systems Developer

Commented:
By the way, using defineGetter for the currentStyle is much better for you than the older syntax because it enables currentStyle as a property of elements rather than as a function, so it's then cross-browser with IE. But I guess you knew that.

Author

Commented:
Odd... yesterday, when I was told about these abilities of Mozilla, I got this line:

HTMLElement.prototype.__defineGetter__("currentStyle", function() { return document.defaultView.getComputedStyle(this, null); });

And I couldn't get it to work. Today, after having pasted your line, and noted the similarities, I again tried the old line. And it did work. Must have done something odd yesterday.

Anyway, thanks for clearing this up!
ahosangFinance Systems Developer

Commented:
Yeh the prototying abilities of Mozilla are awesome!!
Any Object can be prototyped as you know, not just Core Javscript objects! If only people knew about the power of Mozilla.....

Author

Commented:
If only Mozilla could have implemented currentStyle in the first place...

;)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial