Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Node
  4. lookupPrefix()

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.

ThelookupPrefix() method of theNode interfacereturns a string containing the prefix for a given namespace URI, if present,andnull 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. The empty string is equivalent tonull, both causingnull to be returned.

Note:This parameter is not optional but can be set tonull.

Return value

A string containing the corresponding prefix, ornull if none has been found.Ifnamespace is null, or the empty string,lookupPrefix() returnsnull.

If the node is aDocumentType or aDocumentFragment,lookupPrefix() always returnsnull.

Example

Note:This example runs in an HTML document, wherexmlns: attributes are ignored (exceptxmlns:xlink). Firefox sets all elements' namespace URIs tonull, while Chrome and Safari appropriately set HTML, SVG, and MathML elements' default namespace URIs. If you want to conduct more meaningful tests, you can open a standaloneSVG document and execute scripts in its context.

html
<div>  <div>Test HTML element</div>  <svg>    <text>Test SVG element</text>  </svg>  <svg xmlns:xlink="http://www.w3.org/1999/xlink">    <text>Test SVG element with xlink</text>  </svg>  <math>Test MathML element</math></div><table>  <thead>    <tr>      <th><code>namespaceURI</code></th>      <th><code>&lt;div&gt;</code></th>      <th><code>&lt;svg&gt;</code></th>      <th><code>&lt;svg xmlns:xlink&gt;</code></th>      <th><code>&lt;math&gt;</code></th>    </tr>  </thead>  <tbody></tbody></table>
.hidden {  display: none;}
js
const htmlElt = document.querySelector("div");const svgElt = document.querySelector("svg");const svgEltXLink = document.querySelector("#with-xlink");const mathElt = document.querySelector("math");const tbody = document.querySelector("tbody");for (const uri of [  "http://www.w3.org/2000/xmlns/",  "http://www.w3.org/XML/1998/namespace",  "http://www.w3.org/1999/xhtml",  "http://www.w3.org/2000/svg",  "http://www.w3.org/1999/xlink",  "http://www.w3.org/1998/Math/MathML",  "",  null,]) {  const row = document.createElement("tr");  tbody.appendChild(row);  row.appendChild(document.createElement("td")).textContent =    JSON.stringify(uri);  for (const el of [htmlElt, svgElt, svgEltXLink, mathElt]) {    console.log(el, uri, el.lookupPrefix(uri));    row.appendChild(document.createElement("td")).textContent = String(      el.lookupPrefix(uri),    );  }}

Specifications

Specification
DOM
# dom-node-lookupprefix

Browser compatibility

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp