Skip to content

Instantly share code, notes, and snippets.

@JamesMGreene

JamesMGreene/isXhtmlDoc.js

Last active May 10, 2016
Embed
What would you like to do?
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