1
votes

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.

1
Almost all attributes are in the null namespace. - Robert Longson
getAttributeNS require the namespace and attribute, I'm actually looking for the namespace of that attribute... let me edit my exemples - sphinxplayer
And how do you get or retrieve that information Robert ? - sphinxplayer

1 Answers

2
votes

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>