1

By using JavaScript, is there a way to know from which namespace does a specific attribute comes from ?

I can't seem to find anything relevant about this question...

Exemple :

A DIV element with a class attribute. From which namespace comes the class ?

A SVG element with a viewBox attribute. From which namespace comes the viewBox ?

EDIT :

If I wish to set a "xlink:href" attribute on a USE element, I'll have to specify the namespace "http://www.w3.org/1999/xlink".

What I'm looking for is something like : typeof attribute => namespace

So... typeof "xlink:href" => "http://www.w3.org/1999/xlink"

I'm not asking/looking for a complete implementation that would retrieve this information.

3
  • Almost all attributes are in the null namespace. Commented Apr 13, 2018 at 18:28
  • getAttributeNS require the namespace and attribute, I'm actually looking for the namespace of that attribute... let me edit my exemples Commented Apr 13, 2018 at 18:29
  • And how do you get or retrieve that information Robert ? Commented Apr 13, 2018 at 18:30

1 Answer 1

2

You can get the attributes via the attributes attribute and iterate it to get attribute names and namespaces. Most attributes will be in the null namespace.

var attrs = document.getElementById("use").attributes;
for(var i = 0; i < attrs.length; i++) {
    console.log(attrs[i].name + " " + attrs[i].namespaceURI);
}
<svg>
    <use id="use" xlink:href="something" class="something"/>
</svg>

1
  • That is exactly what I'm looking for ! Thanks a lot Robert Commented Apr 13, 2018 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.