Gordian Yuan
Gordian Yuan

Reputation: 5070

How to obtain lang attribute in HTML using JavaScript?

How to obtain lang attribute in HTML using JavaScript?

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

Upvotes: 124

Views: 90785

Answers (2)

Christoph
Christoph

Reputation: 169813

If both attributes agree on their values (as they should), it's enough to read either of them. I'd suggest using

document.documentElement.lang

Upvotes: 249

Christophe Ebl&#233;
Christophe Ebl&#233;

Reputation: 8161

Just.

document.getElementsByTagName('html')[0].getAttribute('lang');

And with the namespace

document.getElementsByTagName('html')[0].getAttribute('xml:lang');

Upvotes: 34

Related Questions