PDA

View Full Version : non-standard attributes via setAttribute


Mark
08.04.2002, 15:07
hello,

in a script i set an attribute, "menuName", with a value. later on i read this attribute. this works fine in ie, but doesn't work in mozilla.
is this coz "menuName" isn't a 'standard' attribute, such as "id"? if so, ie is even more crap than i thought :)

<font color=orange>// Mark</font id=orange>

.:. <font color=brown>it's about the time you can live without your mama </font id=brown>.:. bomfunk mc's

ttrenka
08.04.2002, 15:24
How do you read it? It *should* work in Mozilla if you use element.getAttribute('menuName')...

if you make it all lowercase it may do the trick too.

Chris
08.04.2002, 17:41
yeah, i've made quite a few scripts where i've made up my own attributes...

they all work well in Mozilla, and i've used the technique as ttrenka said.

//-------------{[url="http://chrispoole.com"]Chris Poole[/url:inybym173a]}-------------//
the artist formerly known as fo3nix

Edited by - Chris on 09/04/2002 11:54:17

paul
09.04.2002, 08:13
<BLOCKQUOTE id=quote><font size=1 face="Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>if you make it all lowercase it may do the trick too.<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Arial, Helvetica" size=2 id=quote>

I would suppose this is dependant on the DTD you are using. If it is XHTML then it should be failing, but if it is HTML then this is a bug.

Mark
09.04.2002, 10:34
solved it...

i set the attribute via topEl.menuName and than it doesn't work.
when i set it via topEl.setAttribute("menuName", this.menuName) it works...

... luckily it works, coz otherwase i should have to get this kind of stuff via the id, this works better :)

thanx

<font color=orange>// Mark</font id=orange>

.:. <font color=brown>it's about the time you can live without your mama </font id=brown>.:. bomfunk mc's

ttrenka
09.04.2002, 12:46
Paul--very true, it *should* fail if you are validating against the XHTML DTDs, but the question I would ask is: should the document in question be valid at all points during execution? I mean, if I send out a validated XHTML document, and then use DOM methods to alter that document for the sake of functionality, does it really need to continue to be validated? (edit: I'm referring here to the use of element.getAttribute and element.setAttribute; I don't condone the idea of simply adding an all lower-case attribute directly in the tag if it can be helped)

It seems to me that the spirit of the W3C specs, when dealing with a markup language, is really aimed at init() time. If, during the course of the document's execution, the document is altered using the specification in such a way as to be non-validating, that this is an *implied* acceptable action, if not expressly condoned, by the W3C. There's nothing (at least in both the DOM2 and the XHTML spec) that states that the setAttribute method is limited only to validating attributes, after all.

I like the idea of using setAttribute and getAttribute to add information to an existing document for that reason: it lets the author of a document send out a perfectly valid document, AND allows for, through the specifications, usage that may not have been anticipated by the W3C...this way the doc still degrades very nicely, and we all get the best of both worlds. The nice thing about the way the specification is authored is that it does *not* specify that the document needs to continue to be validated after client-side alteration; it seems that it is mainly concerned with the ability for different clients to be able to recognize the initial document, with the onus on the programmer/scripter to ensure that their code fails gracefully if necessary.

Does this not satisfy both your philosphy and mine?

Edited by - ttrenka on 04/09/2002 10:48:40

Mark
09.04.2002, 13:32
amen

<font color=orange>// Mark</font id=orange>

.:. <font color=brown>it's about the time you can live without your mama </font id=brown>.:. bomfunk mc's

paul
09.04.2002, 14:18
No, I completely disagree.

You are basically saying that after an XHTML document has been parsed by a parser then it ceases to be an XHTML document and even an XML document. I do not see this as true.

The DOM allows for documents to be created and saved, so surely this concept wouldn't work if the documet was no longer valid. The document should continue to be valid throughout it's life if it claims validity.

If you want to add your own attributes then you should add them under your own namespace and use the getAttributeNS and setAttributeNS to set/get them.

Whether the support is there is a different matter. Mozilla has support for namespaced attributes in HTML (this is post 0.9.9). I do not realistically consider Internet Explorer a DOM compliant UA.

I do however think that treating elements as objects and adding properties using the element.foo = "bar"; is perfectly valid as you are not editing the XML document.

ttrenka
09.04.2002, 14:40
Ah, so you like the use of the "expando" concept rather than using attributes. I can live with that...the only thing I don't like about that is that most coders out there tend to wrap XHTML Nodes with their own objects, as opposed to simply working with the Node object itself.

If we only had support for namespaces....I am totally with you on that one. Much cleaner than simply adding attributes willy-nilly.

Well, so much for that debate....seems to be resolved...