You are here: Reference > JavaScript > client-side > xml handling > methods > lookupNamespaceURI

lookupNamespaceURI method

Browser support:
9
Retrieves the namespace URI associated with the specified namespace prefix in the scope of the current node.
Note: Internet Explorer supports the lookupNamespaceURI method from version 9, but only for HTML documents, not for XML documents.
The reverse of this method is the lookupPrefix method; it retrieves the namespace prefix is associated with the specified namespace URI.

Syntax:

object.lookupNamespaceURI (prefix);
You can find the related objects in the Supported by objects section below.

Parameters:

prefix
Required. String that specifies the namespace prefix to search for.

Return value:

String that retrieves the matching namespace URI. If no namespace URI is associated with the specified namespace prefix, it returns null.

Example HTML code 1:

This example illustrates the use of the lookupNamespaceURI method:
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
                    GetNamespaceURI ();
                }
                else {
                    alert ("Operation failed.");
                }
            }
        }

        function GetNamespaceURI () {
            var xmlDoc = ParseHTTPResponse (httpRequest);   // defined in ajax.js
            if (!xmlDoc)
                return;
                
            var firstItem = xmlDoc.getElementsByTagName ("item")[0];
            if (firstItem.lookupNamespaceURI) {
                var nsURI = firstItem.lookupNamespaceURI ("dotto");
                alert ("The namespace URI associated with the 'dotto' prefix: " + nsURI);
            }
            else {
                alert ("Your browser doesn't support the lookupNamespaceURI method.");
            }
        }
    </script>
</head>
<body>
    <button onclick="SendRequest ()">Get the namespace URI associated with the 'dotto' 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