Node: isDefaultNamespace() method
{{APIRef("DOM")}}
The isDefaultNamespace() method of the {{domxref("Node")}} interface accepts a namespace URI as an argument.
It returns a boolean value that is true if the namespace is the default namespace on the given node and false if not. The default namespace can be retrieved with {{domxref("Node.lookupNamespaceURI()")}} by passing null as the argument.
Syntax
isDefaultNamespace(namespaceURI)
Parameters
namespaceURI- : A string representing the namespace against which the element will be checked. The empty string is equivalent to
null.[!NOTE]
namespaceURIis not an optional parameter, but can benull.
- : A string representing the namespace against which the element will be checked. The empty string is equivalent to
Return value
A boolean value that holds the return value true or false, indicating if the parameter is the default namespace, or not. It is equivalent to node.lookupNamespaceURI(null) === namespaceURI.
Example
[!NOTE] This example runs in an HTML document, where
xmlns:attributes are ignored (exceptxmlns:xlink). Firefox sets all elements' namespace URI 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 standalone SVG document and execute scripts in its context.
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.isDefaultNamespace(uri));
row.appendChild(document.createElement("td")).textContent = String(
el.isDefaultNamespace(uri),
);
}
}
{{ EmbedLiveSample('Example','100%',190) }}
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
- {{domxref("Node.lookupNamespaceURI")}}
- {{domxref("Node.lookupPrefix")}}
谢谢你的反馈!