Skip to content

Instantly share code, notes, and snippets.

@josimard
Created October 9, 2014 22:11
Show Gist options
  • Save josimard/ca4cc8f7d80de0c20bf4 to your computer and use it in GitHub Desktop.
Save josimard/ca4cc8f7d80de0c20bf4 to your computer and use it in GitHub Desktop.
Device info utilty
/**
* Device info SINGLETON utilty (AMD wrapped)
* dependencies:
* - jQuery
* - Modernizr + Modernizr.touch
*
* @author jo@josimard.com
*
* Updates @ https://gist.github.com/josimard/ca4cc8f7d80de0c20bf4
*/
define([], function ()
{
var device = {};
var ua = device.ua = navigator.userAgent;
function init()
{
// https://github.com/Modernizr/Modernizr/blob/924c7611c170ef2dc502582e5079507aff61e388/feature-detects/touchevents.js
device.touch = Modernizr.touch;
device.startedOnLandscape = ($(window).innerWidth() > $(window).innerHeight());
if(!device.touch)
{
device.desktop = true;
$("html").addClass("desktop");
}
if (ua.indexOf('iPad')>-1) // && !window.navigator.standalone
{
device.ipad = true;
$('html').addClass('ipad');
// http://stackoverflow.com/questions/7400489/ipad-version-detection-in-javascript
device.version = 0;
window.ondevicemotion = function(e) {
device.version = (e.acceleration) ? 1 + window.devicePixelRatio : 1;
window.ondevicemotion = null;
}
//viewport = document.querySelector("meta[name=viewport]");
//viewport.setAttribute('content', 'width=device-width, maximum-scale=1.0, user-scalable=0');
}
// http://msdn.microsoft.com/en-us/library/ms537509(v=vs.85).aspx
// http://stackoverflow.com/questions/16135814/check-for-ie-10
if (navigator.appName == 'Microsoft Internet Explorer')
{
device.ie = true;
device.version = new Function("/*@cc_on return @_jscript_version; @*/")();
}
// http://tripleodeon.com/2011/12/first-understand-your-screen/
if (ua.toLowerCase().indexOf('android')>-1) // && !window.navigator.standalone
{
device.android = true;
$('html').addClass('android');
}
if (ua.match(/.*CPU.*OS \d_\d/i))
{
device.ios = true;
$('html').addClass('ios');
if (ua.match(/.*CPU.*OS 7_\d/i))
{
device.ios7 = true;
$('html').addClass('ios7');
}
}
$(window).on("resize", onWindowResize);
if(device.touch)
{
$(window).on("orientationchange", onOrientationChange);
onOrientationChange();
} else {
onWindowResize();
}
}
function onWindowResize()
{
update();
}
function onOrientationChange()
{
update();
}
function update()
{
var windowSize = {
width: $(window).innerWidth(),
height: $(window).innerHeight()
};
var isLandscape = (windowSize.width > windowSize.height);
// http://tripleodeon.com/2011/12/first-understand-your-screen/
if(device.touch)
{
// jQuery did not get the right values on iOS4
// http://davidwalsh.name/orientation-change
// http://stackoverflow.com/questions/8205812/jquery-js-ios-4-and-document-height-problems
// http://www.ethanhackett.com/?blog=window-height-100-on-mobile-safari-coding-solution
windowSize = {
width: window.innerWidth,
height: window.innerHeight
}
// Android
// http://moduscreate.com/orientation-change-zoom-scale-android-bug/
if(device.android)
{
}
// http://modernizr.com/docs/#mq
device.isLandscapePhone = Modernizr.mq("(max-width: 720px) and (orientation:landscape)");
}
$.extend(device, {
width: windowSize.width,
height: windowSize.height,
isLandscape: isLandscape,
isPortrait: !isLandscape
});
}
init();
return device;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment