-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkMobileAgent.js
60 lines (57 loc) · 2.03 KB
/
checkMobileAgent.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
function checkMobileAgent() {
// check and return mobile agent
// https://github.com/tibu/checkMobileAgent
var MobileAgent = "(not-detected)";
var Device = {};
Device.Name = "(not set)";
Device.OSVersion = "(not set)";
Device.UA = navigator.userAgent;
Device.Types = ["iPhone", "iPod", "iPad", "Android", "webOS", "BlackBerry"];
for (var d = 0; d < Device.Types.length; d++) {
var t = Device.Types[d];
//match the Type against the User Agent
Device[t] = !!Device.UA.match(new RegExp(t, "i"));
}
// is this an Apple device?
if (Device.iPhone || Device.iPod || Device.iPad) {
//check verion of Apple OS
var iOSVersionRegex = new RegExp (/(OS (?:\d+[\._]?)+)/);
var iOSResult = iOSVersionRegex.exec(Device.UA);
var iOSProduct = "(not set)";
if (Device.iPhone) { iOSProduct = "iPhone"; }
if (Device.iPod) { iOSProduct = "iPod"; }
if (Device.iPad) { iOSProduct = "iPad"; }
if (iOSResult != null) {
for (i = 0; i < iOSResult.length; i++) {
if (iOSResult[i] != undefined) {
Device.OSVersion = iOSResult[i].replace("OS ", "");
}
}
MobileAgent = iOSProduct + ":" + Device.OSVersion;
}
} else if (Device.Android) {
var AndroidVersionRegex = new RegExp (/(Android (?:\d+\S*[\._]?)+)/);
var AndroidResult = AndroidVersionRegex.exec(Device.UA);
if (AndroidResult != null) {
for (i = 0; i < AndroidResult.length; i++) {
if (AndroidResult[i] != undefined) {
Device.OSVersion = AndroidResult[i].replace("Android ", "");
}
}
MobileAgent = "Android:" + Device.OSVersion;
}
} else if (Device.BlackBerry) {
//Older BlackBerry User Agent strings differ from new ones, so we need to check for both formats
var BBVersionRegex = new RegExp (/BlackBerry\d{3,4}[A-Za-z]*\/((?:\d+\.?){2,4})|Version\/((?:\d+\.?){2,4})/);
var BBResult = BBVersionRegex.exec(Device.UA);
if (BBResult != null) {
for (i = 0; i < BBResult.length; i++) {
if (BBResult[i] != undefined) {
Device.OSVersion = BBResult[i];
}
}
MobileAgent = "BlackBerry:" + Device.OSVersion;
}
}
return MobileAgent;
}