JavaScript Node lookupNamespaceURI() Method

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is lookupNamespaceURI()?

node.lookupNamespaceURI(prefix) returns the namespace URI for that prefix on the node, or null if none is found (MDN).

Status: Baseline Widely available (since July 2015) — not Deprecated, Experimental, or Non-standard. In HTML pages, namespace results can differ by browser — prefer createElementNS for clear demos.

Remember
node.lookupNamespaceURI(prefix)
→ URI string | null

null or ""  → default namespace URI
"xml"       → http://www.w3.org/XML/1998/namespace
"xmlns"     → http://www.w3.org/2000/xmlns/
unknown     → null

isDefaultNamespace(uri) ≡
  lookupNamespaceURI(null) === uri
GoalPrefer
URI for a prefixnode.lookupNamespaceURI(prefix)
Read the default URIlookupNamespaceURI(null)
Is this URI the default?isDefaultNamespace(uri)
Find a prefix for a URIlookupPrefix(uri)

How to Use lookupNamespaceURI()

Pass a prefix (or null for the default) and read the URI string.

  1. Get a node (often created with createElementNS).
  2. Choose a prefix — or null / "" for the default.
  3. Call node.lookupNamespaceURI(prefix).
  4. Handle null when the prefix is missing or the node type never resolves.
PieceRequired?Role
nodeYesNode to inspect
prefixYes (may be null)Prefix to resolve; null/"" = default
Return—Namespace URI string, or null

Pseudocode

Pseudocode
if prefix is "xml":
    return "http://www.w3.org/XML/1998/namespace"
if prefix is "xmlns":
    return "http://www.w3.org/2000/xmlns/"
if prefix is null or "":
    return default namespace URI (or null)
if prefix is bound on this node (or ancestors):
    return that URI
else:
    return null

Cheat Sheet

Keep these namespace facts handy.

TopicDetail
Syntaxnode.lookupNamespaceURI(prefix)
ReturnsURI string or null
Defaultnull or "" as prefix
xml / xmlnsFixed URIs (MDN)
EquivalenceisDefaultNamespace(uri) ↔ default URI === uri
Always nullDocumentFragment, DocumentType, empty Document, unbound Attr
StandardBaseline Widely available (since July 2015)

Live Preview

Resolve prefixes on SVG / MathML / HTML nodes in this browser. Use createElementNS demos for clearer results.

Browser demo of Node.lookupNamespaceURI() — HTML results may vary (MDN).

Result
Choose a node and prefix, then Look up…

JavaScript Examples

Short, copy-ready snippets. Prefer createElementNS for stable demos. Use Try It Yourself for a live editor.

Example 1 — Default Namespace (null / "")

MDN: null and empty string both mean the default namespace.

JavaScript
const SVG_NS = "http://www.w3.org/2000/svg";
const svg = document.createElementNS(SVG_NS, "svg");

console.log(svg.lookupNamespaceURI(null)); // SVG_NS
console.log(svg.lookupNamespaceURI(""));   // same as null
console.log(svg.namespaceURI);             // SVG_NS
Try It Yourself

How It Works

  1. Create in the SVG namespace. createElementNS sets the element’s namespace.
  2. Pass null. Looks up the default namespace URI.
  3. Empty string matches. MDN treats "" like null.
  4. Cross-check. namespaceURI should match for that element.

Example 2 — Fixed xml and xmlns URIs

These prefixes always resolve to the same URIs (MDN).

JavaScript
const el = document.createElement("div");

console.log(el.lookupNamespaceURI("xml"));
// http://www.w3.org/XML/1998/namespace

console.log(el.lookupNamespaceURI("xmlns"));
// http://www.w3.org/2000/xmlns/

console.log(el.lookupNamespaceURI("missing")); // null
Try It Yourself

How It Works

  1. xml is reserved. Always the XML namespace URI.
  2. xmlns is reserved. Always the xmlns namespace URI.
  3. Unknown prefix → null. No binding means no URI.
  4. Any node works. These specials do not depend on element type.

Example 3 — Same Question as isDefaultNamespace

MDN: the boolean check equals comparing the default URI.

JavaScript
const SVG_NS = "http://www.w3.org/2000/svg";
const svg = document.createElementNS(SVG_NS, "svg");

const def = svg.lookupNamespaceURI(null);
const a = svg.isDefaultNamespace(SVG_NS);
const b = def === SVG_NS;
console.log(def);
console.log(a, b, a === b); // true true true
Try It Yourself

How It Works

  1. Read the default URI. lookupNamespaceURI(null).
  2. Compare with ===. Same question as isDefaultNamespace.
  3. Both should match. Useful when debugging mismatches.
  4. Pick the API. Use this method when you need the URI string itself.

Edge Cases & Pitfalls

Check these before relying on prefix lookups in HTML pages.

html

HTML documents vary by browser

MDN: xmlns: is mostly ignored in HTML (except xmlns:xlink). Firefox may report null; Chrome/Safari often set HTML/SVG/MathML defaults. Prefer createElementNS or an SVG document.

null

Some node types always return null

MDN: DocumentFragment, DocumentType, Document with no documentElement, or Attr with no associated element.

create

createElement("svg") is not enough

Use createElementNS so the element actually has the SVG namespace.

pair

Pair with related APIs

Boolean default check: isDefaultNamespace. URI → prefix: lookupPrefix.

Browser Support

Node.lookupNamespaceURI() is marked Baseline Widely available on MDN (since July 2015).

✓ Baseline · Widely available

Node.lookupNamespaceURI()

Resolves a namespace prefix to a URI (null/"" = default). Fixed URIs for xml and xmlns. Useful as an XPath namespace resolver. Not Deprecated, Experimental, or Non-standard.

100% Widely available
Google Chrome Yes (Baseline)
Yes
Microsoft Edge Yes (Baseline)
Yes
Mozilla Firefox Yes (Baseline)
Yes
Apple Safari Yes (Baseline)
Yes
Opera Yes (Baseline)
Yes
Internet Explorer Supported (legacy)
Yes
lookupNamespaceURI() Excellent

Bottom line: Safe everyday DOM API for XML/SVG tooling. See the MDN lookupNamespaceURI reference.

Performance Notes

ApproachCostBest for
xml / xmlnsO(1)Fixed reserved URIs
Default / bound prefixCheapTypical SVG / XML nodes
Ancestor walkMay climb parentsDeep trees — cache if called often

Fine for occasional XML/SVG checks. Cache the result if you resolve the same prefix in a loop. Everyday HTML UI rarely needs this API.

Key Takeaways

  • Prefix → URI: returns a string or null.
  • Default: pass null or "".
  • xml / xmlns: fixed URIs every time.
  • Pair APIs: isDefaultNamespace for yes/no; lookupPrefix for the reverse.

One line: lookupNamespaceURI turns a prefix (or null for default) into a namespace URI — or null.

Frequently Asked Questions

It takes a namespace prefix and returns the matching namespace URI on that node, or null if none is found. Passing null (or an empty string) looks up the default namespace.
No. MDN marks Node.lookupNamespaceURI() as Baseline Widely available (since July 2015). It is a standard DOM method — not Deprecated, Experimental, or Non-standard.
MDN: prefix "xml" always returns "http://www.w3.org/XML/1998/namespace". Prefix "xmlns" always returns "http://www.w3.org/2000/xmlns/".
isDefaultNamespace(uri) is equivalent to node.lookupNamespaceURI(null) === uri. Use lookupNamespaceURI(null) when you need the default URI string itself.
MDN: always null for a DocumentFragment, DocumentType, Document with no documentElement, or Attr with no associated element. Unknown prefixes also return null.
It resolves prefixes for XML/SVG tooling and can act as a namespace resolver for XPathEvaluator.createExpression() and evaluate(). Everyday HTML UI rarely needs it.

Next: lookupPrefix()

Go the other way — find a prefix bound to a namespace URI.

lookupPrefix tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful