Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

getAttributeNS

As some browsers do not support getAttributeNS, the following might be used to work on them as well. While namespaced attributes are less common than namespaced elements, some standards such as XLink depend on them.

Note that all Gecko-based browsers (including Firefox) support DOM:element.getAttributeNS. This function is not necessary for Gecko-based browsers.
function getAttributeNSWrapper (thisitem, ns, nsatt) {
    if (thisitem === null) {
        return false;
    }
    if (thisitem.getAttributeNS) {
        return thisitem.getAttributeNS(ns, nsatt);
    }
    else if (ns === null) {
        return thisitem.getAttribute(nsatt);
    }
    else if (ns === 'https://www.w3.org/XML/1998/namespace') { // This is assumed so don't try to get an xmlns for the 'xml' prefix
        return thisitem.getAttribute('xml:'+nsatt); // Prefix must be 'xml' per the specs
    }
    var attrs2, result;
    var attrs = thisitem.attributes;
    var prefixAtt = new RegExp('^(.*):'+nsatt.replace(/\./g, '\\.')+'$'); // e.g., xlink:href  // Find any prefixes with the local-name being searched (escape period since only character (besides colon) allowed in an XML Name which needs escaping)
    for (var j = 0; j < attrs.length; j++) { // thisitem's atts // e.g.,  abc:href, xlink:href
          while (((result = prefixAtt.exec(attrs[j].nodeName)) !== null) &&
                  thisitem.nodeName !== '#document' && thisitem.nodeName !== '#document-fragment') {
            var xmlnsPrefix = new RegExp('^xmlns:'+result[1]+'$'); // e.g., xmnls:xl, xmlns:xlink
            // CHECK HIGHER UP FOR XMLNS:PREFIX
            // Check the current node and if necessary, check for the next matching local name up in the hierarchy (until reaching the document root)
            while (thisitem.nodeName !== '#document' && thisitem.nodeName !== '#document-fragment')  {
              attrs2 = thisitem.attributes;
              for (var i = 0; i < attrs2.length; i++) { // Search for any prefixed xmlns declaration on thisitem which match prefixes found above with desired local name
                if (attrs2[i].nodeName.match(xmlnsPrefix) &&
                      attrs2[i].nodeValue === ns ) { // e.g., 'xmlns:xlink' and 'https://www.w3.org/1999/xlink'
                  return attrs[j].nodeValue;
                }
              }
              thisitem = thisitem.parentNode;
            }
          }
    }
    return ''; // if not found (some implementations return 'null' but this is not standard)
}
alert(getAttributeNSWrapper (someElement, 'https://www.w3.org/1999/xlink', 'href')); // gets xlink:href, xl:href, etc. on current element, conditionally on whether its prefix matches a declared namespace

See also

Document Tags and Contributors

Tags: 
 Contributors to this page: wbamberg, Brettz9, ericjung
 Last updated by: wbamberg,