Element.hasAttributeNS() is an instance method that returns true or false depending on whether a namespaced attribute exists. Learn MDN’s guard pattern, SVG xlink:href, when to prefer hasAttribute() for plain HTML, pairing with getAttributeNS(), and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Returns
boolean
03
Args
URI + localName
04
Missing
false
05
HTML
hasAttribute
06
Status
Baseline widely
Fundamentals
Introduction
Most HTML attributes are checked with hasAttribute("href"). When an attribute belongs to a namespace — common in SVG and XML — you need hasAttributeNS(namespace, localName) instead.
MDN: if you work with HTML documents and do not need to specify the attribute as part of a namespace, use hasAttribute(). Reach for hasAttributeNS() when namespace URIs matter.
💡
Beginner tip
The second parameter is the attribute local name (MDN) — for xlink:href use namespace http://www.w3.org/1999/xlink and local name "href". Store namespace URIs in constants like XLINK_NS.
Element.hasAttributeNS() is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.hasAttributeNS()
Check whether a namespaced attribute exists — returns true or false.
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxSupported · Desktop & Mobile
Yes
Apple SafariSupported · macOS & iOS
Yes
OperaSupported · Modern versions
Yes
Internet ExplorerSupported in legacy IE
Yes
hasAttributeNS()Excellent
Bottom line: Use hasAttributeNS() for SVG and XML namespaced attributes. For ordinary HTML attributes, hasAttribute() is simpler (MDN).
Wrap Up
Conclusion
Element.hasAttributeNS() checks whether a namespaced attribute exists on an element. It is the namespace-aware companion to hasAttribute() and pairs naturally with getAttributeNS().
Use for ordinary HTML when hasAttribute() suffices
Pass "xlink:href" as localName (use "href")
Confuse with reading values (use getAttributeNS)
Hard-code wrong namespace URIs
Assume case rules from HTML apply in XML documents
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.hasAttributeNS()
Namespace-aware existence checks.
5
Core concepts
📝01
Returns
boolean
API
📄02
Args
URI + name
Two
✍️03
SVG
xlink href
Common
✅04
HTML
hasAttr
Simpler
⚡05
Pair
getAttrNS
Value
❓ Frequently Asked Questions
It returns a boolean indicating whether the element has the specified attribute in the given namespace (MDN). It checks existence only, not the value.
No. MDN marks Element.hasAttributeNS() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
namespace — a string with the attribute namespace URI. localName — the attribute local name without prefix (MDN).
MDN: for HTML documents where you do not need a namespace, use hasAttribute(). Use hasAttributeNS() for SVG, XML, or other namespaced attributes.
hasAttributeNS() returns true/false for existence. getAttributeNS() returns the attribute value as a string, or null when missing.
MDN suggests using hasAttributeNS() when you need a clear existence check before reading a namespaced attribute value.
Did you know?
MDN: for HTML documents where you do not need a namespace, use hasAttribute(). Reach for hasAttributeNS() when the attribute belongs to a specific namespace URI such as XLink in SVG.