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.
Node.lookupPrefix()
Syntax
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
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>
const button = document.getElementsByTagName('button')[0]; button.addEventListener("click", function () { const aHtmlElt = document.getElementsByTagName('output')[0]; const aSvgElt = document.getElementsByTagName('svg')[0]; 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
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
lookupPrefix |
1
|
12
|
1
|
9
|
≤12.1
|
3
|
1
|
18
|
4
|
≤12.1
|
1
|
1.0
|
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Node/lookupPrefix