Skip to content

Instantly share code, notes, and snippets.

@r4fx
Last active August 29, 2015 14:18
Show Gist options
  • Save r4fx/6285d022ea48a6758bde to your computer and use it in GitHub Desktop.
Save r4fx/6285d022ea48a6758bde to your computer and use it in GitHub Desktop.
Get user environment
// ---------------------------------------------------------
// USER OS & BROWSER
// ---------------------------------------------------------
var userEnvironment = (function(){
var userOS = "";
var userBrowser = "";
var mobile = {
deviceOrientation: function(){
console.log('orientation:', Math.abs(window.orientation));
if (Math.abs(window.orientation) === 90) {
// Landscape
document.querySelector('body').classList.add('orientation-landscape');
} else {
// Portrait
document.querySelector('body').classList.remove('orientation-landscape');
}
}
};
// Check mobile device orientation and do action
window.onorientationchange = mobile.deviceOrientation();
var system = function () {
if (navigator.appVersion.indexOf("Win") != -1) userOS = "windows";
if (navigator.appVersion.indexOf("Mac") != -1) userOS = "mac";
if (navigator.userAgent.match(/Mobi/)) userOS = "mobile";
document.documentElement.setAttribute('data-os', userOS);
return userOS;
};
var browser = function () {
console.log(navigator.appVersion);
if (navigator.appVersion.indexOf("MSIE") != -1) userBrowser = "oldIE"; // IE =< 10
document.documentElement.setAttribute('data-browser', userBrowser);
};
return {
deviceOrientation: mobile.deviceOrientation,
system: system,
browser: browser
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment