Saturday, March 6, 2010

getAttribute('class') does not work with IE but works with Firefox

While debugging a error in Selenium get_eval function, I faced a strange problem (at least strange to me and probably couple of other folks might be facing this STRANGE problem as well) where my following piece of code was throwing "Expected identifier" exception:

var class = row.getAttribute('class');

Here row was a table row, for which I wanted to get the class attribute to process further.

As class is a reserved word in Javascript and therefore it throws error when used as an identifier in IE, but it works fine on Firefox. Also getAttribute function works good with class in Firefox, but throws error in IE.

So the code was changed to something like below and it worked perfect for IE:

var class_name = row.getAttribute('className');

But now it gave error with Firefox as getAttribute function does not work good with 'className'

So I have to modify code to something like below and it worked perfectly for IE and Firefox:

//Works fine for Firefox
var class_name = row.getAttribute('class');

//If browser is IE, this piece of code returns the correct class name
class_name = class_name ? class_name : row.getAttribute('className');

Hope it helps.

Thanks,
QuickSilver1183
QuickSilver1183@gmail.com

No comments:

Post a Comment