tags:

views:

194

answers:

2

I am trying to get the lang value from the HTML tag, but the current JavaScript I am using doesn't work.

This is the HTML code I am trying to access:

<html  lang="it-IT">

And the Javascript

if(navigator.appName == 'Netscape')
{
    langType = navigator.language;
}
else
{
    langType = navigator.browserLanguage;
}

but in testing I still get the value "EN-us"

Can anyone help?

Thanks!

+2  A: 

try this

var language = document.getElementsByTagName("html")[0].getAttribute("lang");

I haven't tried it, but it should work.

nickyt
Just to clarify, the navigator object is used to access information based on the user agent accessing the page. Like nickyt mentions, you need to access the DOM to get access the attribute you are looking for, not the user agent (unless you are looking for the user's pref!) http://is.gd/o2On
Rob
it'll work, but it's more complicated than it needs to be
Christoph
I agree that document.documentElement.lang is cleaner. I just wasn't aware of this property.
nickyt
Thanks to both of you!
BoredOfBinary
+5  A: 

Use

document.documentElement.lang

As Rob has commented, your code gets the browser's language and not the document's.

Christoph
works for level2 DOM supported browsers. level1 would need the elaborate version of nickyt.
xtofl