¿Cómo obtener el atributo lang en HTML usando JavaScript?


80

¿Cómo obtener langatributos en HTML usando JavaScript?

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

Respuestas:


162

Si ambos atributos coinciden en sus valores (como deberían), basta con leer cualquiera de ellos. Sugeriría usar

document.documentElement.lang

¿Obtiene xml:langel langatributo o el atributo?
Kris Selbekk

2
@KrisSelbekk: debería ser el langatributo, pero no he hecho nada para verificar esto ...
Christoph

Esto no funcionará con xml:lang Ref: Dev mozilla
Jean-François Gagnon

28

Sólo.

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

Y con el espacio de nombres

document.getElementsByTagName('html')[0].getAttribute('xml:lang');
Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.