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

lookupPrefix method

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

Syntax:

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

Parameters:

namespaceURI
Required. String that specifies the namespace URI to search for.

Return value:

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

Example HTML code 1:

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

        function GetNamespacePrefix () {
            var xmlDoc = ParseHTTPResponse (httpRequest);   // defined in ajax.js
            if (!xmlDoc)
                return;
                
            var firstItem = xmlDoc.getElementsByTagName ("item")[0];
            if (firstItem.lookupPrefix) {
                var nsPrefix = firstItem.lookupPrefix ("http://help.dottoro.com/NS");
                alert ("The namespace prefix associated with the 'http://help.dottoro.com/NS' namespace URI: " + nsPrefix);
            }
            else {
                alert ("Your browser doesn't support the lookupPrefix method.");
            }
        }
    </script>
</head>
<body>
    <button onclick="SendRequest ()">Get the prefix associated with the 'http://help.dottoro.com/NS' namespace URI</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content