Thursday, February 10, 2005

Custom attribute with namespace: hasAttributeNS()

A colleague today showed me that he had created a custom attribute in his own namespace. He created a small JavaScript function to sort an XHTML table, which is triggered by the attribute g:sort="yes". An example is on his webpage. The JavaScript file can be seen here.

He uses the hasAttributeNS() JavaScript as specified in the W3C DOM Level 2 specs. Unfortunately only Mozilla and Opera 8 have implemented this. I'm not sure about Safari: does anybody know more about this? I haven't see this function on Quirksmode yet. In any case, IE does NOT support this function.

Jim Ley mentioned that this will result in more complicated JavaScript code, because "IE doesn't have a getAttributeNS - you can only do getAttribute("chicken:feed") - which means you have to manage the namespace handling". I don't know what to think about that. Do you really want to handle namespaces? Or can you just do getAttribute("g:sort") and be happy with that.

Personally I'm pragmatic. I like it when scripts work on IE and Mozilla, and ideally on Opera 8 and Safari 1.2+. So getAttributeNS is elegant, but I wouldn't use it on my site. It's time for a JavaScript library that creates an abstraction layer, so you don't have to worry about all these browser quirks :-)

1 Comments:

Blogger Jep Castelein said...

That's an interesting idea. I quickly browsed Dean's site and it seems that he has only done CSS fixes in IE7. It would be great if it's possible to add functions to the DOM in this way. Does anybody know more about this?

2:00 PM  

Post a Comment

<< Home