Skip to content

Instantly share code, notes, and snippets.

@atesgoral
Created April 1, 2010 18:48
Show Gist options
  • Save atesgoral/352210 to your computer and use it in GitHub Desktop.
Save atesgoral/352210 to your computer and use it in GitHub Desktop.
/**
* 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
Copy link

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