You are here: Reference > JavaScript > client-side > xml handling > properties > namespaceURI (attribute, Element)

namespaceURI property (attribute, Element)

Browser support:
Sets or returns the namespace URI of the current node.
Note: The namespaceURI property always returns "http://www.w3.org/1999/xhtml" for HTML elements.
Note: The namespaceURI property of HTML elements is only supported in Safari, Google Chrome, Firefox from version 3.6 and Internet Explorer from version 9.
To set or return the namespace prefix of a node, use the prefix property. If you need the name of a node, use the localName and baseName properties in XML documents and the nodeName and tagName properties in HTML documents.

Syntax:

object.namespaceURI;
You can find the related objects in the Supported by objects section below.
This property is read-only.

Possible values:

String that specifies or retrieves the namespace URI of the current node.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the namespaceURI property:
Code
ajax.js
ns.xml
<head>
    <script type="text/javascript" src="ajax.js"></script>
    
    <script type="text/javascript">
        var httpRequest = null;
        
        function SendRequest () {
            if (!httpRequest) {
                httpRequest = CreateHTTPRequestObject ();   // defined in ajax.js
            }
            if (httpRequest) {          
                    // The requested file must be in the same domain that the page is served from.
                var url = "ns.xml";
                httpRequest.open ("GET", url, true);    // async
                httpRequest.onreadystatechange = OnStateChange;
                httpRequest.send (null);
            }
        }

        function OnStateChange () {
            if (httpRequest.readyState == 0 || httpRequest.readyState == 4) {
                if (IsRequestSuccessful (httpRequest)) {    // defined in ajax.js
                    GetTagInfo ();
                }
                else {
                    alert ("Operation failed.");
                }
            }
        }

        function GetTagInfo () {
            var xmlDoc = ParseHTTPResponse (httpRequest);   // defined in ajax.js
            if (!xmlDoc)
                return;

            if (xmlDoc.getElementsByTagNameNS) {    // Firefox, Opera, Google Chrome and Safari
                var titleTags = xmlDoc.getElementsByTagNameNS ("http://help.dottoro.com/NS", "title");
                if (titleTags.length > 0) {
                    var message = "The localName of the first title tag: " + titleTags[0].localName + "\n" +
                                   "The namespaceURI of the first title tag: " + titleTags[0].namespaceURI + "\n" +
                                   "The prefix of the first title tag: " + titleTags[0].prefix;
                    alert (message);
                }
                else {
                    alert ("There is no title tag!");
                }
            }
            else {      // Internet Explorer
                var titleTags = xmlDoc.getElementsByTagName ("dotto:title");
                if (titleTags.length > 0) {
                    var message = "The localName of the first title tag: " + titleTags[0].baseName + "\n" +
                                   "The namespaceURI of the first title tag: " + titleTags[0].namespaceURI + "\n" +
                                   "The prefix of the first title tag: " + titleTags[0].prefix;
                    alert (message);
                }
                else {
                    alert ("There is no title tag!");
                }
            }
        }

    </script>
</head>
<body>
    <button onclick="SendRequest ()">Get localName, namespaceURI and prefix</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content