Last active
April 23, 2017 08:30
-
-
Save osvik/cc0f1e776edd83c21417224f276f69a4 to your computer and use it in GitHub Desktop.
Add browser languages as CSS classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint browser: true*/ | |
/** | |
* Adds the content of navigator.languages or navigator.language as classes to body | |
* This script must be added after the opening <body> tag. | |
* @author Osvaldo Gago | |
*/ | |
(function () { | |
/** | |
* Adds a classname to an element | |
* @param {object} element Node to add the CSS class to | |
* @param {string} nameClass Name of the CSS class | |
*/ | |
var addClass = function (element, nameClass) { | |
if (element.classList) { | |
element.classList.add(nameClass); | |
} else { | |
element.className += ' ' + nameClass; | |
} | |
}; | |
/** | |
* Main. | |
*/ | |
var body = document.body; | |
if (typeof (navigator.languages) === "object") { | |
for (var language in navigator.languages) { | |
addClass(body, navigator.languages[language]); | |
} | |
} else { | |
addClass(body, navigator.language); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment