Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    wac
    wac is offline
    Regular Coder wac's Avatar
    Join Date
    Sep 2002
    Location
    Cary, North Carolina, USA
    Posts
    359
    Thanks
    2
    Thanked 0 Times in 0 Posts

    accessing non-standard attributes via javascript

    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) ;
    </script>

    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?
    Wayne Christian

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    IIRC, something like document.getElementById('test').attributes['myblah'].value; might work...
    *this message will self destruct in n-seconds*

  • #4
    Regular Coder
    Join Date
    Oct 2004
    Posts
    168
    Thanks
    0
    Thanked 5 Times in 5 Posts
    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:

    document.getElementById('test').getAttribute('myblah');

  • #5
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...
    *this message will self destruct in n-seconds*

  • #6
    wac
    wac is offline
    Regular Coder wac's Avatar
    Join Date
    Sep 2002
    Location
    Cary, North Carolina, USA
    Posts
    359
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanx, everyone. That's what I needed to know, that there was a W3C approved way of getting these.
    Wayne Christian

  • #7
    Regular Coder
    Join Date
    Oct 2004
    Posts
    168
    Thanks
    0
    Thanked 5 Times in 5 Posts
    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.

  • #8
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I got it; but that doesn't work for "non-standard" attributes, IIRC.
    *this message will self destruct in n-seconds*

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Last edited by brothercake; 02-12-2005 at 11:30 AM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #10
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...
    btw, I'd just have to puke before repeating that a 4th time!
    Last edited by codegoboom; 02-12-2005 at 01:33 PM.
    *this message will self destruct in n-seconds*

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #12
    wac
    wac is offline
    Regular Coder wac's Avatar
    Join Date
    Sep 2002
    Location
    Cary, North Carolina, USA
    Posts
    359
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?
    Last edited by wac; 02-14-2005 at 02:44 PM.
    Wayne Christian

  • #13
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Namespaces are an XML thing, yeah. Check out this thread - a guy who's doing the same thing, using namespaces: Quirks Mode, Doctype, Namespaces and extensions
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •