Last active
August 29, 2015 14:18
-
-
Save r4fx/6285d022ea48a6758bde to your computer and use it in GitHub Desktop.
Get user environment
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
// --------------------------------------------------------- | |
// 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