Skip to content

Instantly share code, notes, and snippets.

@brettz9
Created December 13, 2012 05:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brettz9/4274369 to your computer and use it in GitHub Desktop.
Save brettz9/4274369 to your computer and use it in GitHub Desktop.
IE8 Shim for DOMImplementation.prototype.createDocument (does not support doctype argument nor wrap the resulting document to support all DOM methods and properties; mostly for loading XML)
/*globals DOMImplementation, ActiveXObject */
if (!DOMImplementation.prototype.createDocument) {
(function () {
'use strict';
var i, docObj, docObjType,
docObjs = [
'MSXML6.DOMDocument', 'MSXML5.DOMDocument', 'MSXML4.DOMDocument',
'MSXML3.DOMDocument', 'MSXML2.DOMDocument.5.0', 'MSXML2.DOMDocument.4.0',
'MSXML2.DOMDocument.3.0', 'MSXML2.DOMDocument', 'MSXML.DomDocument',
'Microsoft.XmlDom'
],
dol = docObjs.length;
for (i=0; i < dol; i++) {
try {
docObj = new ActiveXObject(docObjs[i]);
docObjType = docObjs[i]; // Set this after ActiveXObject to ensure only set if no errors thrown
break;
}
catch (e) {
}
}
/**
* This shim implementation does not provide any support for doctype, nor does it wrap the resulting
* document so as to support all current DOM methods and properties
*/
DOMImplementation.prototype.createDocument = function (namespace, qualifiedName, doctype) {
if (doctype) {
throw 'This is not a complete shim for ' +
'DOMImplementation.prototype.createDocument.';
}
if (!docObjType) {
throw 'Could not create a DOM document object';
}
var pos, prefix,
prefixedNs = '',
doc = new ActiveXObject(docObjType);
if (qualifiedName) {
pos = qualifiedName.indexOf(':');
if (pos > -1) {
prefix = qualifiedName.slice(0, pos);
prefixedNs = ' xmlns:' + prefix + '="' + namespace + '"';
}
else if (namespace) {
prefixedNs = ' xmlns="' + namespace + '"';
}
doc.loadXML('<' + qualifiedName + prefixedNs + '/>');
}
return doc;
};
}());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment