Skip to content

Instantly share code, notes, and snippets.

@JamesMGreene
Last active May 10, 2016 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesMGreene/edd63391fbac07126f37e81d99d0a34f to your computer and use it in GitHub Desktop.
Save JamesMGreene/edd63391fbac07126f37e81d99d0a34f to your computer and use it in GitHub Desktop.
Simple function to determine if a given Document object in JavaScript represents an XHTML document.
//
// References important to this particular implementation:
// - http://ejohn.org/blog/nodename-case-sensitivity/
// - http://help.dottoro.com/ljdgsrle.php
// - http://www.w3schools.com/html/html_xhtml.asp
// - http://www.w3schools.com/tags/tag_doctype.asp
//
// Other related references if additional parameters are ever needed:
// - http://reference.sitepoint.com/javascript/Document/doctype
// - http://reference.sitepoint.com/javascript/DocumentType
// - http://help.dottoro.com/ljlsvbgj.php
// - http://nimbupani.com/the-truth-about-doctypes.html
//
// Implementation explanation:
// - If a document is being rendered in HTML mode, then all `.nodeName` properties
// return their values in all uppercase (e.g. `HTML`).
// - If a document is being rendered in XML/XHTML mode, then all `.nodeName` properties
// return their values in their original case. However, valid XHTML also requires all
// element names to be specified as lowercase, so the original case MUST always be
// lowercase (e.g. `html`).
function isXhtmlDoc( doc ) {
var _doc = doc || ( typeof document !== 'undefined' && document );
return _doc && _doc.documentElement && _doc.documentElement.nodeName === 'html';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment