Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
You are here: Reference > JavaScript > client-side > HTML DOM > methods > getAttributeNS

getAttributeNS method

Browser support:
9
Returns the value of the attribute with the specified namespace and name from the current element.
Note: Internet Explorer supports the getAttributeNS method from version 9, but only for HTML documents, not for XML documents.
To specify an attribute with namespace, use the setAttributeNS, setAttributeNodeNS or setNamedItemNS method.

Syntax:

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

Parameters:

namespaceURI
Required. String that specifies the namespace URI of the attribute to retrieve. The namespace URI is case-sensitive.
attributeName
Required. String that specifies the name of the attribute to retrieve. The name is case-sensitive.

Return value:

If no attribute exists with the specified namespace and name, it returns an empty string in Firefox, Google Chrome and Safari and null in Opera, else it returns the value of the matching attribute node. To detect whether an attribute is specified, use the hasAttributeNS method.

Methods for attributes with namespaces:

Name Browser Description
createAttributeNS
9
Creates a new attribute node with the specified namespace and name.
getAttributeNS
9
Returns the value of the attribute with the specified namespace and name from the current element.
getAttributeNodeNS
9
Returns the attribute node with the specified namespace and name from the current element.
getNamedItemNS
9
Returns the attribute node with the specified namespace and name from the current attributes collection.
hasAttributeNS
9
Returns whether the current element has an attribute with the specified namespace and name or not.
removeAttributeNS
9
Removes the attribute with the specified namespace and name from the current element.
removeAttributeNode
Removes the specified attribute node from the current element.
removeNamedItemNS
9
Removes the attribute with the specified namespace and name from the current attributes collection and returns the removed attribute node.
setAttributeNS
9
Adds an attribute with the specified namespace, name and value to the current element.
setAttributeNodeNS
9
Adds the specified attribute node to the current element.
setNamedItemNS
9
Adds the specified attribute node to the current attributes collection.

Example HTML code 1:

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

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

            var itemTags = xmlDoc.getElementsByTagName ("item");
            var firstItem = itemTags[0];
            if (firstItem.getAttributeNS) {
                var attrNS = firstItem.getAttributeNS ("http://help.dottoro.com/NS", "color");
                alert ("The color of the first item: " + attrNS);
            }
            else {
                alert ("Your browser doesn't support the getAttributeNS method.");
            }
        }
    </script>
</head>
<body>
    <button onclick="SendRequest ()">Test the getAttributeNS method</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content