Node: lookupPrefix() method
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The lookupPrefix()
method of the Node
interface
returns a string containing the prefix for a given namespace URI, if present,
and null
if not.
When multiple prefixes are possible, the first prefix is returned.
Syntax
js
lookupPrefix(namespace)
Parameters
namespace
-
A string containing the namespace to look the prefix up.
Note: This parameter is not optional but can be set to
null
.
Return value
A string containing the corresponding prefix, or null
if none has been found.
If namespace
is null, or the empty string, lookupPrefix()
returns null
.
If the node is a DocumentType
or a DocumentFragment
,
lookupPrefix()
always returns null
.
Example
html
Prefix for <code>http://www.w3.org/2000/svg</code> on <output>:
<output>Not tested</output>.<br />
Prefix for <code>http://www.w3.org/XML/1998/namespace</code> on <output>:
<output>Not tested</output>.<br />
Prefix for <code>http://www.w3.org/TR/html4/</code> on <output>:
<output>Not tested</output>.<br />
Prefix for <code>https://www.w3.org/1999/xlink</code> on <output>:
<output>Not tested</output>.<br />
Prefix for <code>http://www.w3.org/2000/svg</code> on <svg>:
<output>Not tested</output>.<br />
Prefix for <code>https://www.w3.org/1999/xlink</code> on <svg>:
<output>Not tested</output>.<br />
Prefix for <code>http://www.w3.org/XML/1998/namespace</code> on <svg>:
<output>Not tested</output>.<br />
<svg xmlns:t="http://www.w3.org/2000/svg" height="1"></svg>
<button>Click to see the results</button>
js
const button = document.querySelector("button");
button.addEventListener("click", () => {
const aHtmlElt = document.querySelector("output");
const aSvgElt = document.querySelector("svg");
const result = document.getElementsByTagName("output");
result[0].value = aHtmlElt.lookupPrefix("http://www.w3.org/2000/svg"); // true
result[1].value = aHtmlElt.lookupPrefix(
"http://www.w3.org/XML/1998/namespace",
); // false
result[2].value = aHtmlElt.lookupPrefix("http://www.w3.org/TR/html4/"); // true
result[3].value = aHtmlElt.lookupPrefix("https://www.w3.org/1999/xlink"); // false
result[4].value = aSvgElt.lookupPrefix("http://www.w3.org/2000/svg"); // true
result[5].value = aSvgElt.lookupPrefix("https://www.w3.org/1999/xlink"); // true
result[6].value = aSvgElt.lookupPrefix(
"http://www.w3.org/XML/1998/namespace",
); // false
});
Specifications
Specification |
---|
DOM Standard # dom-node-lookupprefix |
Browser compatibility
BCD tables only load in the browser