Package Exports
- ismobilejs
- ismobilejs/isMobile
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ismobilejs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
isMobile
A simple JS library that detects mobile devices.
Big thanks to Igor Ribeio Lima for his contributions.
Why use isMobile?
I had a specific requirement for a project when I created this:
- Redirect all iPhones, iPods, Android phones, and seven inch devices to the mobile site.
A completely separate site had been created for mobile devices, so feature detection/graceful degredation/progressive enhancement were out of the question. I had to redirect.
I couldn't do detection on the back-end, because the entire site was cached and served by Akamai; I had to do the detection client-side.
So I resorted to UA sniffing.
I tried to keep the script small (currently ~1.4k bytes, minified) and simple, because it would need to execute in the <head>
, which is generally a bad idea, since JS blocks downloading and rendering of anything else while it parses and executes. In the case of mobile redirection, I don't mind so much, because I want to start the redirect as soon as possible, before the device has a chance to start downloading and rendering stuff. For non-mobile platforms, the script should execute fast, so the browser can quickly get back to downloading and rendering.
How it works
isMobile runs quickly on page load to detect mobile devices; it then creates a JavaScript object with the results.
Devices detected by isMobile
The following properies of the isMobile
object will either be true
or false
Apple devices
isMobile.apple.phone
isMobile.apple.ipod
isMobile.apple.tablet
isMobile.apple.device
(any mobile Apple device)
Android devices
isMobile.android.phone
isMobile.android.tablet
isMobile.android.device
(any mobile Android device)
Windows devices
isMobile.windows.phone
isMobile.windows.tablet
isMobile.windows.device
(any mobile Windows device)
Specific seven inch devices
isMobile.seven_inch
true
if the device is one of the following 7" devices:- Nexus 7
- Kindle Fire
- Nook Tablet 7 inch
- Galaxy Tab 7 inch
"Other" devices
isMobile.other_blackberry_10
isMobile.other_blackberry
isMobile.other_opera
(Opera Mini)isMobile.other_firefox
Aggregate Groupings
isMobile.any
- any device matchedisMobile.phone
- any device in the 'phone' groups aboveisMobile.tablet
- any device in the 'tablet' groups above
Example Usage
I include the minified version of the script, inline, and at the top of the <head>
. Cellular connections tend to suck, so it would be wasteful overhead to open another connection, just to download ~1.4kb of JS:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script>
// Minified version of isMobile included in the HTML since it's small
!function(a){var b=/iPhone/i,c=/iPod/i,d=/iPad/i,e=/(?=.*\bAndroid\b)(?=.*\bMobile\b)/i,f=/Android/i,g=/IEMobile/i,h=/(?=.*\bWindows\b)(?=.*\bARM\b)/i,i=/BlackBerry/i,j=/BB10/i,k=/Opera Mini/i,l=/(?=.*\bFirefox\b)(?=.*\bMobile\b)/i,m=new RegExp("(?:Nexus 7|BNTV250|Kindle Fire|Silk|GT-P1000)","i"),n=function(a,b){return a.test(b)},o=function(a){var o=a||navigator.userAgent,p=o.split("[FBAN");return"undefined"!=typeof p[1]&&(o=p[0]),this.apple={phone:n(b,o),ipod:n(c,o),tablet:!n(b,o)&&n(d,o),device:n(b,o)||n(c,o)||n(d,o)},this.android={phone:n(e,o),tablet:!n(e,o)&&n(f,o),device:n(e,o)||n(f,o)},this.windows={phone:n(g,o),tablet:n(h,o),device:n(g,o)||n(h,o)},this.other={blackberry:n(i,o),blackberry10:n(j,o),opera:n(k,o),firefox:n(l,o),device:n(i,o)||n(j,o)||n(k,o)||n(l,o)},this.seven_inch=n(m,o),this.any=this.apple.device||this.android.device||this.windows.device||this.other.device||this.seven_inch,this.phone=this.apple.phone||this.android.phone||this.windows.phone,this.tablet=this.apple.tablet||this.android.tablet||this.windows.tablet,"undefined"==typeof window?this:void 0},p=function(){var a=new o;return a.Class=o,a};"undefined"!=typeof module&&module.exports&&"undefined"==typeof window?module.exports=o:"undefined"!=typeof module&&module.exports&&"undefined"!=typeof window?module.exports=p():"function"==typeof define&&define.amd?define("isMobile",[],a.isMobile=p()):a.isMobile=p()}(this);
// My own arbitrary use of isMobile, as an example
(function () {
var MOBILE_SITE = '/mobile/index.html', // site to redirect to
NO_REDIRECT = 'noredirect'; // cookie to prevent redirect
// I only want to redirect iPhones, Android phones and a handful of 7" devices
if (isMobile.apple.phone || isMobile.android.phone || isMobile.seven_inch) {
// Only redirect if the user didn't previously choose
// to explicitly view the full site. This is validated
// by checking if a "noredirect" cookie exists
if ( document.cookie.indexOf(NO_REDIRECT) === -1 ) {
document.location = MOBILE_SITE;
}
}
})();
</script>
</head>
<body>
<!-- imagine lots of html and content -->
</body>
</html>
node.js usage
#####Installation
npm install ismobilejs
#####Usage
var isMobile = require('ismobilejs');
console.log(isMobile(req.headers['user-agent']).any);