document.createAttributeNS() creates a new namespaced attribute node (Attr) with a namespaceURI and qualifiedName. Creating the node does not attach it — set value, then use setAttributeNode() (MDN).
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call document.createAttributeNS. Start with this line.
Prefix needs a URI.xml: is a prefix, so ns cannot be "".
Call throws. The engine raises NamespaceError.
Catch err.name. Expect "NamespaceError".
Fix it. Use the XML URI, or drop the prefix for null-ns names.
Compatibility
Browser Support
Document.createAttributeNS() is Baseline Widely available (MDN). It is not Deprecated, Experimental, or Non-standard.
✓ Baseline · Widely available
Document.createAttributeNS()
Supported in every modern browser. Prefer setAttributeNS for everyday namespaced sets.
99%Modern browser support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
document.createAttributeNS()Excellent
Bottom line: Safe everywhere. Focus on correct namespace URIs and when you need an Attr node versus setAttributeNS.
Frequently Asked Questions
MDN: Document.createAttributeNS() creates a new attribute node with a specified namespace URI and qualified name. The result is an Attr object — set attr.value, then attach with setAttributeNode() or setAttributeNodeNS().
No. MDN marks Document.createAttributeNS() as Baseline Widely available. It is not Deprecated, Experimental, or Non-standard.
Use createAttributeNS() when the attribute belongs to a namespace — for example xml:lang (XML namespace) or xlink:href (XLink). For ordinary HTML attributes in the null namespace, createAttribute() or setAttribute() is enough.
MDN: in HTML documents most attributes are in the null namespace — pass the empty string "" for namespaceURI when creating unprefixed attributes like SVG viewBox.
MDN: a string like prefix:localName or just localName. For xml:lang, qualifiedName is "xml:lang" and namespaceURI is http://www.w3.org/XML/1998/namespace.
Yes. For most apps, element.setAttributeNS(namespace, name, value) is simpler than createAttributeNS + attach. Use createAttributeNS when you need an Attr node object.