HTML has gone quite a long way since its inception. This means a lot of new features but also some small incompatibilities which may pose issues in certain situations. For instance, when posting a code snippet for others to include on their websites, it’s best if it works correctly on as many sites as possible which implies being compatible with as many versions of HTML as possible. But how to create a snippet that works both in HTML and XHTML? Here are a few tips:
JavaScript
- Convert
tagName to lowercase, also applies to nodeName. In HTML they return uppercase names but in XHTML they return lowercase names. Using tagName.toLowerCase() unifies the values. - Specify namespace. Use
createElementNS instead of createElement if available. Same goes for getElementsByTagNameNS and {create,set,get,has,remove}AttributeNS. you happen to use it.
Afterword
It needs to be noted that the above rules are not exhaustive and do not include things that everyone is doing anyway (like using id attribute for fragment identifiers instead of (just) name), have limited application to code snippets (like styling of html and body differences), or produce only minor differences (like handling of white-space in attributes or leading line feed in pre). Neither does this entry advertise writing whole documents in something that appears to be valid HTML4, XHTML and HTML5 (but for HTML5 which is valid XML see polyglot markup), even though it does advertise writing JavaScript and CSS code that works in all those recommendations and their various variants.