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

lookupPrefix method

Browser support:
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.


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


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:
    <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";
       ("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)
            var firstItem = xmlDoc.getElementsByTagName ("item")[0];
            if (firstItem.lookupPrefix) {
                var nsPrefix = firstItem.lookupPrefix ("");
                alert ("The namespace prefix associated with the '' namespace URI: " + nsPrefix);
            else {
                alert ("Your browser doesn't support the lookupPrefix method.");
    <button onclick="SendRequest ()">Get the prefix associated with the '' namespace URI</button>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content