forked from kaimallea/isMobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isMobile.js
97 lines (79 loc) · 3.44 KB
/
isMobile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* isMobile.js v0.3.2
*
* A simple library to detect Apple phones and tablets,
* Android phones and tablets, other mobile devices (like blackberry, mini-opera and windows phone),
* and any kind of seven inch device, via user agent sniffing.
*
* @author: Kai Mallea (kmallea@gmail.com)
*
* @license: http://creativecommons.org/publicdomain/zero/1.0/
*/
(function (global) {
var apple_phone = /iPhone/i,
apple_ipod = /iPod/i,
apple_tablet = /iPad/i,
android_phone = /(?=.*\bAndroid\b)(?=.*\bMobile\b)/i, // Match 'Android' AND 'Mobile'
android_tablet = /Android/i,
windows_phone = /IEMobile/i,
windows_tablet = /(?=.*\bWindows\b)(?=.*\bARM\b)/i, // Match 'Windows' AND 'ARM'
other_blackberry = /BlackBerry/i,
other_opera = /Opera Mini/i,
other_firefox = /(?=.*\bFirefox\b)(?=.*\bMobile\b)/i, // Match 'Firefox' AND 'Mobile'
seven_inch = new RegExp(
'(?:' + // Non-capturing group
'Nexus 7' + // Nexus 7
'|' + // OR
'BNTV250' + // B&N Nook Tablet 7 inch
'|' + // OR
'Kindle Fire' + // Kindle Fire
'|' + // OR
'Silk' + // Kindle Fire, Silk Accelerated
'|' + // OR
'GT-P1000' + // Galaxy Tab 7 inch
')', // End non-capturing group
'i'); // Case-insensitive matching
var match = function(regex, userAgent) {
return regex.test(userAgent);
};
var IsMobileClass = function(userAgent) {
var ua = userAgent || navigator.userAgent;
this.apple = {
phone: match(apple_phone, ua),
ipod: match(apple_ipod, ua),
tablet: match(apple_tablet, ua),
device: match(apple_phone, ua) || match(apple_ipod, ua) || match(apple_tablet, ua)
};
this.android = {
phone: match(android_phone, ua),
tablet: !match(android_phone, ua) && match(android_tablet, ua),
device: match(android_phone, ua) || match(android_tablet, ua)
};
this.windows = {
phone: match(windows_phone, ua),
tablet: match(windows_tablet, ua),
device: match(windows_phone, ua) || match(windows_tablet, ua)
};
this.other = {
blackberry: match(other_blackberry, ua),
opera: match(other_opera, ua),
firefox: match(other_firefox, ua),
device: match(other_blackberry, ua) || match(other_opera, ua) || match(other_firefox, ua)
};
this.seven_inch = match(seven_inch, ua);
this.any = this.apple.device || this.android.device || this.windows.device || this.other.device || this.seven_inch;
// excludes 'other' devices and ipods, targeting touchscreen phones
this.phone = this.apple.phone || this.android.phone || this.windows.phone;
// excludes 7 inch devices, classifying as phone or tablet is left to the user
this.tablet = this.apple.tablet || this.android.tablet || this.windows.tablet;
};
var IM = new IsMobileClass();
IM.Class = IsMobileClass;
if (typeof module != 'undefined' && module.exports) {
module.exports = IM;
} else if (typeof define === 'function' && define.amd) {
define(IM);
} else {
global.isMobile = IM;
}
})(this);