HTML - DOM Element namespaceURI Property



The HTML DOM Element namespaceURI property is used to retrieve the namespace URI of an element in the document. It returns "null" if the given element is not a part of the namespace.

A namespace URI in HTML is a Uniform Resource Identifier (URI) used to define the namespace for elements and attributes in an XML or XHTML document.

Syntax

Following is the syntax of the HTML DOM Element namespaceURI property −

element.namespaceURI

Parameters

Since this is a property, it will not accept any parameter.

Return Value

This property returns a string that contains the URI of the element's namespace or null if the element is not part of any namespace.

Example 1: Getting namespace URI of Element

The following is the basic example of the HTML DOM Element namespaceURI. It retrieves the namespace URI of an element in the document −

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML DOM Element namespaceURI</title>
</head>
<body>
<h3>HTML DOM Element namespaceURI Property</h3>
<div id="myDiv">Hello, World!</div> 
<div id="result"></div>
<script>
   const divElement = document.getElementById('myDiv');
   const namespaceURI = divElement.namespaceURI; 
   // Display the namespace URI in the result element
   const resultElement = document.getElementById('result');
   resultElement.textContent = `Namespace URI of #myDiv: ${namespaceURI}`;
</script>
</body>
</html>     

The above program displays the namespace URI of the "div" element.

Example 2: Getting namspace URI of Element in XHTML

Here is another example of the HTML DOM Element namespaceURI property. We use this property to retrieve the namespace URI of an SVG element −

<!DOCTYPE html>
<html>
<head>
<title>HTML DOM Element namespaceURI</title>
</head>
<body>
<h3>HTML DOM Element namespaceURI Property</h3>
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
<circle id="myCircle" cx="50" cy="50" r="40" stroke="black" stroke-width="2" 
fill="red" />
</svg>
<p id="result"></p>
<script>
   const circleElement = document.getElementById('myCircle');
   const namespaceURI = circleElement.namespaceURI;
   const resultElement = document.getElementById('result');
   // Updates the HTML content
   resultElement.textContent = `Namespace URI of #myCircle: 
   ${namespaceURI}`;
</script>
</body>
</html>        

Once the above program is executed, it displays the name-space URI of the SVG element.

Supported Browsers

Property Chrome Edge Firefox Safari Opera
namespaceURI Yes Yes Yes Yes Yes
html_dom.htm
Advertisements