tagName vs nodeName
nodeName is preferred if you are supporting only A-grade browsers and
tagName is preferred if you intend to support IE5.5 as well.
There are two issues with
- In all versions of IE,
!when called on a comment node
- For text nodes,
undefinedwhere as nodeName returns
nodeName has it’s own set of issues but they are less severe:
- IE 5.5 returns
!when called on a comment node. This is less harmful than
tagNamewhich suffers from this behaviour across all versions of IE
- IE 5.5 doesn’t support
documentelement or for attributes. Neither of these should be a concern for most practical purposes but should be kept in mind in any case
- Konqueror ignores comment nodes when using this property. But then again, Konqueror, along with IE 5.5 is not an A-grade browser
So for most practical purposes stick to nodeName due to its support for a wider range of scenarios and potentially better forward compatibility. Not to mention that it doesn’t hiccup on a comment node, which has a tendency to creep into code unannounced. Don’t worry about IE 5.5 or Konqueror as their market share is near 0%.