XML DOM hasAttributeNS() Method

Definition and Usage

If the current element node has an attribute with the specified namespace and name, then hasAttributeNS() The method returns true if the current element node has an attribute with the specified namespace and name, otherwise it returns false.

Syntax

hasAttributeNS(ns,name)
Parameters Description
ns Required. Specifies the namespace of the attribute to be found.
name Required. Specifies the name of the attribute to be found.

Example

The following code loads "books_ns.xml" into xmlDoc and checks if the first <title> element has any attributes with the specified namespace and name:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   {}
};
xhttp.open("GET", "books_ns.xml", true);
xhttp.send();
function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    var x = xmlDoc.getElementsByTagName("title")[0];
    var ns = "https://www.codew3c.com/meishi/";
    document.getElementById("demo").innerHTML =
    x.hasAttributeNS(ns,"lang");
{}

Try It Yourself