Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Hack to allow jQuery to work within XHTML documents that define an xmlns
*/
/**
* Use the given object to override the given methods in its prototype
* with namespace-aware equivalents
*/
function addNS(obj, methods) {
var proto = obj.constructor.prototype;
var ns = document.documentElement.namespaceURI;
for (var methodName in methods) {
(function () {
var methodNS = proto[methodName + "NS"];
if (methodNS) {
proto[methodName] = function () {
var args = Array.prototype.slice.call(arguments, 0);
args.unshift(ns);
return methodNS.apply(this, args);
};
}
})();
}
}
// Play nice with IE -- who doesn't need this hack in the first place
if (document.constructor) {
// Override document methods that are used by jQuery
addNS(document, {
createElement: 1,
getElementsByTagName: 1
});
// Override element methods that are used by jQuery
addNS(document.createElement("div"), {
getElementsByTagName: 1,
getAttribute: 1,
getAttributeNode: 1,
removeAttribute: 1,
setAttribute: 1
});
}
@joelisfunk

This comment has been minimized.

Copy link

joelisfunk commented Mar 22, 2012

Can you provide a usage example? For example, how would I use this to read a fb:loginbutton tag with jQuery?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.