node.lookupPrefix(namespace) returns a prefix bound to that namespace URI 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, xmlns: is mostly ignored (except xmlns:xlink) — prefer createElementNS demos.
Remember
node.lookupPrefix(namespaceURI)
→ prefix string | null
null or "" as argument → always null
(default namespace has a URI, but no prefix)
lookupNamespaceURI: prefix → URI
lookupPrefix: URI → prefix
multiple prefixes? → first one (MDN)
Pass a namespace URI and read the prefix string (or null).
Get a node that may have namespace bindings (often SVG).
Pass the namespace URI you care about (not null).
Call node.lookupPrefix(uri).
Treat null as “no prefix” — including for the default namespace.
Piece
Required?
Role
node
Yes
Node to inspect
namespace
Yes (may be null)
URI to look up; null/"" → null
Return
—
Prefix string, or null
Pseudocode
Pseudocode
if namespace is null or "":
return null
if node is DocumentType or DocumentFragment:
return null
if a prefix is bound to that URI:
return the first matching prefix
else:
return null // includes default NS (no prefix)
Reference
Cheat Sheet
Keep these prefix facts handy.
Topic
Detail
Syntax
node.lookupPrefix(namespace)
Returns
Prefix string or null
null / "" arg
Always returns null (MDN)
Default namespace
Has a URI, usually no prefix
Multiple prefixes
First match (MDN)
Always null nodes
DocumentType, DocumentFragment
Standard
Baseline Widely available (since July 2015)
Try it
Live Preview
Resolve URIs to prefixes on an SVG node with an xlink binding.
Namespace URI
Browser demo of Node.lookupPrefix() — default namespaces usually have no prefix.
Result
Choose a URI, then Look up…
Code
JavaScript Examples
Short, copy-ready snippets. Prefer createElementNS + explicit xmlns: bindings. Use Try It Yourself for a live editor.
Example 1 — Find the xlink Prefix
Bind xmlns:xlink on SVG, then resolve the XLink URI.
Bind xmlns:xlink. Associates the XLink URI with prefix xlink.
Look up XLink. Returns "xlink".
Look up SVG URI. Default namespaces have no prefix → null.
Example 2 — null and Empty String
MDN: both argument values make lookupPrefix return null.
JavaScript
const el = document.createElement("div");
console.log(el.lookupPrefix(null)); // null
console.log(el.lookupPrefix("")); // null
// Not like lookupNamespaceURI(null) — that reads the default URI
Start with a prefix.lookupNamespaceURI("xlink") → URI.
Go back.lookupPrefix(uri) → "xlink".
Bound names round-trip. Prefixed bindings work both ways.
Default NS does not. SVG URI often has no prefix to return.
Edge Cases & Pitfalls
Check these before expecting a prefix for every namespace URI.
default
Default namespace has no prefix
A URI can be the default and still return null from lookupPrefix. Read the URI with lookupNamespaceURI(null) instead.
args
null / "" always return null
Unlike lookupNamespaceURI, these arguments do not mean “default namespace” here (MDN).
nodes
DocumentFragment / DocumentType
MDN: lookupPrefix() always returns null on these node types.
html
HTML ignores most xmlns:
MDN: except xmlns:xlink. Prefer setAttributeNS + createElementNS, or test in a standalone SVG document.
Compatibility
Browser Support
Node.lookupPrefix() is marked Baseline Widely available on MDN (since July 2015).
✓ Baseline · Widely available
Node.lookupPrefix()
Resolves a namespace URI to a bound prefix (or null). null/"" arguments return null. Inverse of lookupNamespaceURI. Not Deprecated, Experimental, or Non-standard.
Fine for occasional XML/SVG checks. Cache the prefix if you resolve the same URI in a loop. Everyday HTML UI rarely needs this API.
Remember
Key Takeaways
URI → prefix: returns a string or null.
null/"" args: always null — not the default namespace.
Default NS: has a URI, usually no prefix.
Inverse pair: use with lookupNamespaceURI.
One line:lookupPrefix turns a namespace URI into a short prefix — or null if none is bound.
Frequently Asked Questions
It takes a namespace URI and returns a bound prefix string on that node, or null if none is found. When multiple prefixes are possible, the first prefix is returned.
No. MDN marks Node.lookupPrefix() as Baseline Widely available (since July 2015). It is a standard DOM method — not Deprecated, Experimental, or Non-standard.
MDN: the empty string is equivalent to null, and both cause lookupPrefix() to return null. Unlike lookupNamespaceURI, null does not mean “default namespace” here.
lookupNamespaceURI goes prefix → URI. lookupPrefix goes URI → prefix. They are inverse lookups. A default namespace has a URI but no prefix, so lookupPrefix often returns null for that URI.
MDN: if the node is a DocumentType or DocumentFragment, lookupPrefix() always returns null. Unbound URIs, and null/empty arguments, also return null.
Useful in XML/SVG tooling when you know a URI and need the short prefix used in markup, or when debugging prefixed attributes like xlink:href.