sphinxplayer
sphinxplayer

Reputation: 330

JavaScript : NameSpace of a specific attribute?

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.

Upvotes: 1

Views: 334

Answers (1)

Robert Longson
Robert Longson

Reputation: 124089

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>

Upvotes: 2

Related Questions