Skip to content

Instantly share code, notes, and snippets.

@arv
Created March 13, 2014 14:55
Show Gist options
  • Select an option

  • Save arv/9529994 to your computer and use it in GitHub Desktop.

Select an option

Save arv/9529994 to your computer and use it in GitHub Desktop.
Implementation of createAttributeNS and setAttributeNodeNS
if (!Document.prototype.createAttributeNS) {
Document.prototype.createAttributeNS = function(namespaceURI, qualifiedName) {
var dummy = this.createElement('dummy');
dummy.setAttributeNS(namespaceURI, qualifiedName, '');
var attr = dummy.attributes[0];
dummy.removeAttributeNode(attr);
return attr;
};
}
if (!Element.prototype.setAttributeNodeNS) {
Element.prototype.setAttributeNodeNS = Element.prototype.setAttributeNode;
}
var attr = document.createAttributeNS('http://www.w3.org/1999/xlink', 'href');
attr.nodeValue = 'http://example.com';
console.dir(attr);
var test = document.createElement('test');
test.setAttributeNodeNS(attr);
console.dir(test);
@RathoreShubham

Copy link
Copy Markdown

Great work working .... saved me :-)

@zcorpan

zcorpan commented Apr 15, 2014

Copy link
Copy Markdown

removeAttributeNode is also removed from http://dom.spec.whatwg.org/

@zcorpan

zcorpan commented Apr 15, 2014

Copy link
Copy Markdown

(Use removeAttributeNS instead)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment