Warning: This method can take attribute values that are parsed as HTML, a script, or as a script URL, depending on the attribute. APIs like this are known as injection sinks, and are potentially a vector for cross-site scripting (XSS) attacks, if the value originally came from an attacker.
You can mitigate this risk by always passing the appropriate trusted type object (TrustedHTML, TrustedScript, or TrustedScriptURL) instead of strings for those attributes that require them, and enforcing trusted types. See Security considerations in Element.setAttribute() for more information.
The setAttributeNS() method of the Element interface adds a new attribute or changes the value of an attribute with the given namespace and name.
If you are working with HTML documents and you don't need to specify the requested attribute as being part of a specific namespace, use the setAttribute() method instead.
setAttributeNS(namespaceURI, qualifiedName, value)namespaceURIA string specifying the namespace of the attribute to set, or the empty string.
qualifiedNameA string identifying the attribute by its qualified name, which has the format prefix:localName or localName, where the parts are defined as:
prefixA "short alias" for the namespace. The prefix is optional, but if it is specified the namespaceURI parameter must also be specified. If the prefix is set to xml or xmlns, the namespaceURI must be set to http://www.w3.org/XML/1998/namespace or http://www.w3.org/2000/xmlns/, respectively.
localNameThe local name of the attribute.
valueA trusted type or string containing the value to assign to the attribute.
Trusted type instances must be passed for the following attributes when trusted types are enforced:
onclick and onload, require a TrustedScript.HTMLIFrameElement.srcdoc require a TrustedHTML instance.HTMLScriptElement.src require a TrustedScriptURL instance.SVGScriptElement.href require a TrustedScriptURL instance.Trusted types are not enforced for other attributes, so a string or any trusted type may be passed.
None (undefined).
NamespaceError DOMExceptionThrown if the namespaceURI value is:
prefix has a value.http://www.w3.org/XML/1998/namespace or http://www.w3.org/2000/xmlns/ when prefix is set to xml or xmlns, respectively.InvalidCharacterError DOMExceptionThrown if either the prefix or localName is not valid:
prefix must have at least one character, and cannot contain ASCII whitespace, NULL, /, or > (U+0000, U+002F, or U+003E, respectively).localName must have at least one character, and may not contain ASCII whitespace, NULL, /, = or > (U+0000, U+002F, U+003D, or U+003E, respectively).Note: Earlier versions of the specification were more restrictive, requiring that the qualifiedName be a valid XML name.
TypeErrorThrown if value is passed a string instead of a trusted type object (for those attributes that require them) when Trusted Types are enforced by a CSP and no default policy is defined.
let d = document.getElementById("d1");
d.setAttributeNS(
"http://www.mozilla.org/ns/specialspace",
"spec:align",
"center",
);The Setting unsafe attributes example in setAttribute() shows how you might use setAttributeNS() with the trusted types.