summaryrefslogtreecommitdiffstats
path: root/static/functions/detect_mobile.js
blob: 3e2d7d072ed60ea32b0c4ccaba25f051514a556f (plain)
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
var ANDROID_COOKIE_NAME = "mobile_checked_android";
var OTHER_COOKIE_NAME = "mobile_checked_other";
var MOBILE_SITE_HOSTNAME = "m.what.cd";
var MOBILE_SITE_URL = "https://m.what.cd/";
var ANDROID_APP_URL = "http://bit.ly/whatandroid";

var isMobile = {
	Android: function() {
		return navigator.userAgent.match(/Android/i) ? true : false;
	},
	BlackBerry: function() {
		return navigator.userAgent.match(/BlackBerry/i) ? true : false;
	},
	iOS: function() {
		return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true : false;
	},
	Windows: function() {
		return navigator.userAgent.match(/IEMobile/i) ? true : false;
	},
	Any: function() {
		return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());
	},
	NotAndroid: function() {
	return (isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());
	}
};

if (window.location.hostname == MOBILE_SITE_HOSTNAME) {
	setCookie(OTHER_COOKIE_NAME, true, 365);
} else {
	if (isMobile.Android()) {
		if (!hasCookie(ANDROID_COOKIE_NAME)) {
			setCookie(ANDROID_COOKIE_NAME, true, 365);
			var result = confirm("An Android app is available for What.CD. Would you like to download it?");
			if (result == true) {
				window.location = ANDROID_APP_URL;
			}
		}
	} else if (isMobile.NotAndroid()) {
		if (!hasCookie(OTHER_COOKIE_NAME)) {
			setCookie(OTHER_COOKIE_NAME, true, 365);
			var result = confirm("A mobile version of What.CD is available. Would you like to use it?");
			if (result == true) {
				window.location = MOBILE_SITE_URL;
			}
		}
	}
}

function setCookie(c_name, value, exdays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
	document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
	var i, x, y, ARRcookies = document.cookie.split(";");
	for (i = 0; i < ARRcookies.length; i++) {
		x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
		x = x.replace(/^\s+|\s+$/g,"");
		if (x == c_name) {
			return unescape(y);
		}
	}
}

function hasCookie(c_name) {
	var checked = getCookie(c_name);
	if (checked != null) {
		return true;
	}
	return false;
}