Skip to content

Instantly share code, notes, and snippets.

@osvik
Last active April 23, 2017 08:30
Show Gist options
  • Save osvik/cc0f1e776edd83c21417224f276f69a4 to your computer and use it in GitHub Desktop.
Save osvik/cc0f1e776edd83c21417224f276f69a4 to your computer and use it in GitHub Desktop.
Add browser languages as CSS classes
/*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