Friday, August 7, 2009

Wikinews js and IE

So i happened to be browsing Wikinews in internet explorer the other day (*shudder*), and apparently there was an error in some of the site JS (relating to the comments namespace) making some of the js not work in IE. The issue was that internet explorer doesn't support the hasAttribute method of dom elements. Which isn't that bad since its fairly trivial to replace hasAttribute with getAttribute. The scary thing is that this issue has been present since November 2007.

Apparently (looking through the history) there was an attempt to fix back in January of 2008, by changing hasAttribute('missing') to getAttribute('missing', 2) == "". (I'm not sure where the 2 came from. All the docs i've read seem to state that getAttribute only takes 1 parameter) This almost would have worked, except for one important fact. In the mediawiki api, if you try to find info on a non-exisistant page, it will give you: <page ... missing="" /> [1] Thus the normal value for the attribute if present is "", and if its not present, according to the w3c getArribute should return the empty string. Thus testing for the empty string doesn't work, as it returns the empty string in both cases. However in practice this doesn't seem entirely true, as in both Firefox and IE, when getAttribute is used on a missing attribute, the browser returns null instead of "" (The previous code of getAttribute('missing', 2) == "" would still of worked since "" == null)

To fix the problem, the code in question now first tries to use the hasAttribute() method (for the good little browsers that support it), and if that throws an exception, it will see if getAttribute(attributeName) !== null. (assuming that anybody who doesn't support hasAttribute also returns null when getting a non-existant attribute.

Anyways, the moral of this story is that we should probably set up some system for tracking problems with the local javascript on wikinews, since this problem was discovered and than totally forgotten. perhaps on [[Wikinews:Javascript]].

No comments:

Post a Comment