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 :-)


Blogger Grauw said...

My reply got so extensive that I decided to post it on my own blog :):

3:46 PM  
Blogger Grauw said...

Btw, about that abstraction layer... I wonder, surely someone must have created it already? Perhaps, perhaps not, or maybe everybody just re-invents the wheel for himself and uses his own code.


3:49 PM  
Blogger Grauw said...

Another p.s.:

"so you don't have to worry about all these browser quirks :-)"

It isn’t really a quirk at all. IE does not parse XHTML documents as XML. Therefor, the concept of namespaces mean nothing to it. It is probably the same for Mozilla when it parses the XHTML document as HTML with the text/html MIME type.

By the way, this script and a previous one creating email links (which I want to improve someday) are both part of an effort to make more scripts for XML/XHTML sites available. Well, not really an ‘effort’ as such, I just want these little things for my own site, and the existing resources usually use HTML-only solutions employing document.write and innerHTML, so I have to write my own versions. Which I publish in the hopes that some other people will find it useful.


3:57 PM  
Blogger alias420 said...

Well and what about using IE7 fix ? maybe that dean can come up with a fix for NS as well ;-)

12:16 PM  
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  
Blogger Grauw said...

Afaik IE7 is about CSS only, indeed.

By the way, I just tried to make it work in IE. Appears the problem isn’t just that IE doesn’t support the *NS functions. In addition to that, it also doesn’t support hasAttribute, nor addEventListener, and probably more. I think I might need to use IE’s proprietary attachEvent, but that seems to work different, or maybe I need to let the script manually place an onclick attribute on the element... Who knows.

Basically, this is where I gave up :). It’s never as easy as it seems, and I figured this would happen... Sorry IE users, I’d rather keep my script clean.

10:10 AM  
Blogger Grauw said...

p.s. I did make the page degrade nicer (without JavaScript error), checking for support of the used functions (funny that Opera 7.5 claims support for all *NS functions). And while I was at it, added a little indication in the status bar which mentions the lacking functionality and which browsers do support it. Maybe that’s a bit over the top though, I dunno :).


10:41 AM  

Post a Comment

<< Home