View Full Version : accessing non-standard attributes via javascript

02-10-2005, 04:35 PM
I know the title says javascript but this is more of a combination question.
On Windows IE browser, if I code an HTML tag like so
(I'm picking span, but this could be ANY tag)
I can set an arbitrary attribute and retrieve the value from javascript

<span id='test' myblah='wow' >stuff </span>

<script type='text/javascript'>
alert('value of span.myblah = '+ document.getElementById('test').myblah) ;

Is this behavior that can be counted to on other browsers (NS6+, FireFox) and
other platforms (Linux, Mac)??? Or should I stay away from doing this?

02-10-2005, 04:53 PM
Definitely not reliable. Opera aren't even allowing this kind of access for some attributes that have no bindings listed in the DOM spec.

The way to read an attribute is through the Element.prototype.getAttribute method.

02-10-2005, 04:59 PM
IIRC, something like document.getElementById('test').attributes['myblah'].value; might work...

Puffin the Erb
02-10-2005, 08:13 PM
wac, what you are using is born from Microsoft's nasty, proprietary document.expando property which lets you access non-standard attributes.

To develop what liorean mentioned you could use this W3C friendly code:


02-10-2005, 10:29 PM
Wait a minute... isn't that basically the same thing?

NamedNodeMap objects in the DOM are live.

"attributes" is a namedNodeMap of attributes on the current element.

I seem to remember element.attributes["itemName"] being the only way to get/set nasty property values between IE/Moz/Op, but uh... :D

02-10-2005, 10:51 PM
Thanx, everyone. That's what I needed to know, that there was a W3C approved way of getting these.

Puffin the Erb
02-11-2005, 07:46 PM
My point is that document.expando is Microsoft only, whereas the W3C approach works cross-browser (including IE).

I see proprietary as nasty and cross-browser as not nasty. :thumbsup:

02-12-2005, 08:15 AM
I got it; but that doesn't work for "non-standard" attributes, IIRC.

02-12-2005, 11:27 AM
element.expando works for any property if it exists (if it's not null) - so element.id will always work, because an HTML element always has a implicit ID attribute value, even if it's an empty string.

.className is the same - except in konqueror. And there are many other examples - a link always has ".href", an img always has ".src", and so on

But if you try it on an attribute that doesn't exist, it won't work. Therefore for testing if an attribute value is present at all, getAttribute('foobar') is the right approach, returning null if it doesn't exist.

btw - using custom attributes like that - what do you about validation? Wouldn't it be better to use namespaced attributes?

02-12-2005, 11:44 AM
Yeah, but invalid (user-defined) attributes don't exist as such; yet, they can be manipulated in those browsers through the attributes collection, as opposed to when using get/setAttribute, unless I'm mis-remembering... :confused:

btw, I'd just have to puke before repeating that a 4th time! :p

02-14-2005, 12:31 AM
I'll take your word for it. I've heard of similar things - IE5, for example, can only access the content document of a page in an iframe if you access it through the frames[] array

02-14-2005, 02:29 PM
btw - using custom attributes like that - what do you about validation? Wouldn't it be better to use namespaced attributes?

If that was directed at me, I have minimal HTML knowledge (as you may have already surmised). I don't know what namespaces are or what their appropriate applications would be. I get frequent headaches after browsing the W3C legaleeze for HTML and CSS. (Or is namespace more of an XML thing?) Basically, one of the developers just wanted to associate some data to an HTML tag. Is Namespace something that all/most browsers are likely to support?

02-19-2005, 05:23 PM
Namespaces are an XML thing, yeah. Check out this thread - a guy who's doing the same thing, using namespaces: http://www.codingforums.com/showthread.php?t=52674