Skip to content

Instantly share code, notes, and snippets.

@vitalyrotari
Created August 2, 2012 12:41
Show Gist options
  • Save vitalyrotari/3236756 to your computer and use it in GitHub Desktop.
Save vitalyrotari/3236756 to your computer and use it in GitHub Desktop.
Detect Mobile Devices
var Device = {
ENV_DESKTOP: 'desktop',
ENV_PHONE: 'phone',
ENV_TABLET: 'tablet',
ENV_PORTRAIT: 'portrait',
ENV_LANDSCAPE: 'landscape',
agent: {
mobile: (/iphone|ipod|android|blackberry|opera|mini|windows\sce|palm|smartphone|iemobile/i.test(window.navigator.userAgent.toLowerCase())),
tablet: (/ipad|android|android\s3\.0|xoom|sch-i800|playbook|tablet|kindle/i.test(window.navigator.userAgent.toLowerCase()))
},
resolutions: {
phones: {
width: [240, 960],
height: [320, 800]
},
tablets: {
width: [1024, 2048],
height: [60, 1536]
}
},
support: {
orientation: ('DeviceOrientationEvent' in window),
motion: ('DeviceMotionEvent' in window),
touch: ('ontouchstart' in window)
},
standalone: ('standalone' in window.navigator),
detect: function() {
var that = this,
inRange = function (type) {
var size = that.screenSize(),
collection = that.resolutions[type];
return (
(size.width >= collection.width[0] && size.width <= collection.width[1]) &&
(size.height >= collection.height[0] && size.height <= collection.height[1])
);
};
if (this.agent.mobile && inRange('phones')) {
this.type = this.ENV_PHONE;
} else {
this.type = (this.agent.tablet && inRange('tablets'))
? this.ENV_TABLET
: this.ENV_DESKTOP;
}
},
orientation: function() {
return this.support.orientation
? (window.orientation !== 0 ? this.ENV_PORTRAIT : this.ENV_LANDSCAPE)
: null;
},
screenSize: function() {
var portrait = (this.orientation() === this.ENV_PORTRAIT);
return {
width: portrait ? screen.height : screen.width,
height: portrait ? screen.width : screen.height
};
},
hideKeyboard: function() {
document.activeElement.blur();
},
hideUrlBar: function() {
if (this.type === this.ENV_PHONE) {
setTimeout(function() {
window.scrollTo(0, 1);
}, 0);
}
}
};
Manufacturer Model Screen Size Resolution Type
----------------------------------------------------------------------------
Acer Iconia Tab 10.1″ 1280×800 Tablet
Amazon Kindle Fire 7″ 1024×600 Tablet
Apple iPad 9.7″ 1024×768 Tablet
Apple iPad 2 9.7″ 1024×768 Tablet
Apple iPad 3 9.7″ 2048×1536 Tablet
Apple iPhone 3GS 3.5″ 480×320 Smartphone
Apple iPhone 4S 3.5″ 960×640 Smartphone
ASUS Transformer TF101 10.1″ 1280×800 Tablet
ASUS Transformer Prime TF201 10.1″ 1280×800 Tablet
Barnes & Noble NOOK 7″ 1024×600 Tablet
BlackBerry Playbook 7″ 1024×600 Tablet
BlackBerry Torch 9800 3.2″ 480×360 Smartphone
BlackBerry Torch 9810 3.2″ 640×480 Smartphone
Colby Kyros 7″ 800×480 Tablet
HP TouchPad 9.7″ 768×1024 Tablet
HTC Amaze 4G 4.3″ 960×540 Smartphone
HTC Evo 3D 4.3″ 960×540 Smartphone
HTC Flyer 7″ 1024×600 Tablet
HTC Sensation 4G 4.3″ 960×540 Smartphone
HTC Thunderbolt 4.3″ 480×800 Smartphone
Lenovo IdeaPad A1 7″ 1024×600 Tablet
Lenovo ThinkPad 10.1″ 1280×800 Tablet
LG G2x 4G 4″ 800×400 Smartphone
LG G-Slate 8.9″ 768×1280 Tablet
Motorola Droid BIONIC 4.3″ 540×960 Smartphone
Motorola Droid RAZR 4.3″ 540×960 Smartphone
Motorola Droid Xyboard 10.1 10.1″ 1280×800 Tablet
Motorola Xoom 10.1″ 1280×800 Tablet
Nokia Lumia 900 4.3″ 480×800 Smartphone
Samsung Brightside 3.1″ 240×320 Smartphone
Samsung Epic 4G Touch 4.52″ 800×480 Smartphone
Samsung Exhibit 4G 3.5″ 480×800 Smartphone
Samsung Focus Flash 3.7″ 480×800 Smartphone
Samsung Galaxy S 4G 4″ 480×800 Smartphone
Samsung Galaxy S II 4.52″ 800×480 Smartphone
Samsung Galaxy Tab 7″ 1024×600 Tablet
Samsung Galaxy Tab 7.7 7.7″ 1280×800 Tablet
Samsung Galaxy Tab 10.1 10.1″ 1280×800 Tablet
Samsung Gravity Smart 3.2″ 320×480 Smartphone
Samsung Sidekick 4G 3.5″ 800×480 Smartphone
Sony Tablet S 9.4″ 1280×800 Tablet
T-Mobile MyTouch 4G Slide 3.8″ 800×480 Smartphone
T-Mobile SpringBoard 7″ 1280×800 Tablet
Toshiba Thrive 10.1″ 1280×800 Tablet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment