Knoten: Methode lookupNamespaceURI()

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.

Die lookupNamespaceURI() Methode der Node Schnittstelle nimmt ein Präfix als Parameter und gibt den Namensraum-URI zurück, der mit diesem auf dem angegebenen Knoten assoziiert ist, falls er gefunden wird (und null, wenn nicht). Die Existenz dieser Methode ermöglicht es, Node Objekte als Namensraum-Resolver an XPathEvaluator.createExpression() und XPathEvaluator.evaluate() zu übergeben.

Syntax

js
lookupNamespaceURI(prefix)

Parameter

prefix

Das zu suchende Präfix.

Hinweis: Dieser Parameter ist nicht optional, kann aber auf null gesetzt werden.

Rückgabewert

Ein String, der den Namensraum-URI enthält, der dem Präfix entspricht.

  • Gibt immer null zurück, wenn der Knoten ein DocumentFragment, DocumentType, Document ohne documentElement oder Attr ohne zugehöriges Element ist.
  • Wenn prefix "xml" ist, ist der Rückgabewert immer "http://www.w3.org/XML/1998/namespace".
  • Wenn prefix "xmlns" ist, ist der Rückgabewert immer "http://www.w3.org/2000/xmlns/".
  • Wenn das prefix null ist, ist der Rückgabewert der Standard-Namensraum-URI.
  • Wenn das Präfix nicht gefunden wird, ist der Rückgabewert null.

Beispiel

html
<div style="display: none">
  <div>Test HTML element</div>
  <svg>
    <text>Test SVG element</text>
  </svg>
  <math>Test MathML element</math>
</div>

<table>
  <thead>
    <tr>
      <th><code>prefix</code></th>
      <th><code>&lt;div&gt;</code></th>
      <th><code>&lt;svg&gt;</code></th>
      <th><code>&lt;math&gt;</code></th>
    </tr>
  </thead>
  <tbody></tbody>
</table>
js
const htmlElt = document.querySelector("div");
const svgElt = document.querySelector("svg");
const mathElt = document.querySelector("math");

const tbody = document.querySelector("tbody");

for (const prefix of ["xmlns", "xml", "html", "svg", "xlink", "", null]) {
  const row = document.createElement("tr");
  tbody.appendChild(row);
  row.appendChild(document.createElement("td")).textContent =
    JSON.stringify(prefix);
  for (const el of [htmlElt, svgElt, mathElt]) {
    console.log(el, prefix, el.lookupNamespaceURI(prefix));
    row.appendChild(document.createElement("td")).textContent = String(
      el.lookupNamespaceURI(prefix),
    );
  }
}

Spezifikationen

Specification
DOM
# dom-node-lookupnamespaceuri

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
lookupNamespaceURI

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Siehe auch