getAttributeNS

Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.

From Firefox 53 onwards, no new legacy add-ons will be accepted on addons.mozilla.org (AMO).

From Firefox 57 onwards, WebExtensions will be the only supported extension type, and Firefox will not load other types.

Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to WebExtensions if they can. See the "Compatibility Milestones" document for more.

A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.

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 === 'http://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 'http://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, 'http://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: bunnybooboo, wbamberg, Brettz9, ericjung
 Last updated by: bunnybooboo,