is there any way to use something like IE's expando properties in
Mozilla?
Assume I have a button which is used to toggle the visibility of some
DOM item. With IE I can write:
<button toggles="div7">toggle div7</button>
With IE I can read the toggles-property of the button in a script, e.g.:
for (var i=0;i<document.getElementsByTagName("button").length;i++){
var el=document.getElementsByTagName("button")[i];
if (el.toggles){
el.onclick=function(){
var divCSS=document.getElementById(this.toggles).style;
divCSS.visibility=(divCSS.visibility=="hidden")?
"visible" : "hidden";
}
}
}
This is nice. However, in Mozilla, I only get "undefined" for
el.toggles.
Is there a reason why this isn't implemented? Does the W3C say that it
is not allowed to extend the properties of HTML elements?
It seems strange to me that
aButtonElement.toggles=17;
works, but
aButtonElement.setAttribute("toggles",17);
does not.
Wolfgang.
Expandos should work (check
http://bugzilla.mozilla.org/show_bug.cgi?id=50533 though).
As for setAttribute / getAttribute, they refer to the 'source file', and not
to the 'running model'.
(I'm sure Vidur will jump in here if this explanation in incomplete.)
If you still have problems, files a bug.
Hope that helps.
"Wolfgang Schwarz" <wolfgang...@tool42.com> wrote in message
news:39AB7C73...@tool42.com...
In Mozilla, for each HTML or XML element visible to script, there is a native
(C++) object that holds the attribute values and a JavaScript object that acts
as its proxy in the script world. For attributes and methods explicitly
specified in the DOM interfaces, property access and methods calls on the
JavaScript object get translated to equivalent calls on the C++ object. For
example, setting the "src" property on the JS obect results in a call to the
SetSrc() method on the C++ object and a modification to the attribute structure
that it holds.
For properties that are not part of the DOM interface of an element, property
access or modification does not result in calls to the C++ object. Instead, the
property value is stored in the JavaScript object itself. This is because the
JavaScript object model allows the addition of new properties on a per object
basis.
To retrieve or modify markup attributes, an author should use getAttribute and
setAttribute. To deal with custom properties in the JavaScript world only, an
author can access or change properties directly on JS object itself. The only
exception to this is the set of HTML attributes specified in the W3C spec.
--Vidur
So "custom attributes" should be accessed via getAttribute(), right?
> In Mozilla, for each HTML or XML element visible to script, there is a
native
> (C++) object that holds the attribute values and a JavaScript object that
acts
> as its proxy in the script world. For attributes and methods explicitly
> specified in the DOM interfaces, property access and methods calls on the
> JavaScript object get translated to equivalent calls on the C++ object.
For
> example, setting the "src" property on the JS obect results in a call to
the
> SetSrc() method on the C++ object and a modification to the attribute
structure
> that it holds.
>
> For properties that are not part of the DOM interface of an element,
property
> access or modification does not result in calls to the C++ object.
Instead, the
> property value is stored in the JavaScript object itself. This is because
the
> JavaScript object model allows the addition of new properties on a per
object
> basis.
Ok, so expandos will be thrown away when you call cloneNode() on a Node
reference that has expandos set?
> To retrieve or modify markup attributes, an author should use getAttribute
and
> setAttribute. To deal with custom properties in the JavaScript world only,
an
> author can access or change properties directly on JS object itself. The
only
> exception to this is the set of HTML attributes specified in the W3C spec.
Vidur, what do you think about custom properties in stylesheets? Will I be
able to access them for an element using the getComputedStyle() in some way?
IE5 allows access to custom CSS properties via the "currentStyle" property,
and I use that a lot.
Thanks in advance for any further explanation.
> So "custom attributes" should be accessed via getAttribute(), right?
Yes.
> Ok, so expandos will be thrown away when you call cloneNode() on a Node
> reference that has expandos set?
Right. JavaScript properties will not be cloned. Markup attributes will be.
> Vidur, what do you think about custom properties in stylesheets? Will I be
> able to access them for an element using the getComputedStyle() in some way?
Hmm...I believe the DOM expects CSS custom properties to be accessible via the
CSSStyleSheet-rooted hierarchy, but I don't think we said anything about custom
properties through getComputedStyle(). My guess is that, given our
implementation, Mozilla will not be able to return custom property values in the
computed case. I think j...@netscape.com might be able to come up with a more
definitive answer.
--Vidur