Skip to content

Instantly share code, notes, and snippets.

@mturnwall
Last active December 25, 2015 07:49
Show Gist options
  • Save mturnwall/6941819 to your computer and use it in GitHub Desktop.
Save mturnwall/6941819 to your computer and use it in GitHub Desktop.
A simple function to determine if a device is iPad, iPhone, or Android. The version of the OS is also returned.
var deviceIs = (function () {
var userAgent = navigator.userAgent,
device = {
type: (userAgent.match(/iphone|android|ipad/i)) ? userAgent.match(/iphone|android|ipad/i)[0] : false,
fullVersion: false,
majorVersion: false
};
if (device.type) {
device.fullVersion = userAgent.match(/(OS|android)\s+([\d(\_|\.)]+)/i)[0].replace(/_/g,'.');
device.majorVersion = device.fullVersion.match(/[0-9]/)[0];
}
return device;
})();
@mturnwall
Copy link
Author

Here's an example of what the deviceIs properties will look like:

deviceIs.type  // 'iPhone'
deviceIs.fullVersion // '7.0.2'
deviceIs.majorVersion // '7'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment