This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser.survey.js
173 lines (144 loc) · 4.54 KB
/
browser.survey.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/** @preserve
* Detects which experience to serve to a user based on the client browser.
*
* Github: https://github.com/knation/browser-survey
*
* Version 0.0.1
*
* Kirk Morales (http://www.kirkmorales.com)
* Copyright 2013. All Rights Reserved.
* @license MIT LICENSE
*/
(function(window, document, navigator) {
"use strict";
/**
* Default properties for the experiences we can render.
* @type {object.<string, object>}
* @static
*/
var EXPIERIENCES = {
'phone': { 'defaultMaxWidth': 480 },
'tablet': { 'defaultMaxWidth': 1024 },
'desktop': { }
};
/**
* Regular expression for known tablet devices.
* @type {RegExp}
* @static
*/
var KNOWN_TABLETS = new RegExp('iPad|Kindle|Silk|PlayBook', 'i');
/**
* Regular expression for known phone devices.
* @type {RegExp}
* @static
*/
var KNOWN_PHONES = new RegExp('iPhone|iPod|BlackBerry', 'i');
/**
* Regular expression for known mobile devices (generic).
* @type {RegExp}
* @static
*/
var KNOWN_MOBILE = new RegExp('Android', 'i');
/**
* The page body element.
* @type {?DOMElement}
*/
var BODY;
/**
* Gets the screen width. Returns null if it can't be detected.
* @return {?number}
*/
function getWidth() {
if (document && document.documentElement && document.documentElement.clientWidth) {
return document.documentElement.clientWidth;
} else if (window) {
if (window.innerWidth) {
return window.innerWidth;
} else if (window.outerWidth) {
return window.outerWidth;
}
}
return null;
}
/**
* Called when the screen is resized.
*/
function resized() {
var experience;
// If we have a user agent, test against our regular expressions
if (navigator && navigator.userAgent) {
if (KNOWN_PHONES.test(navigator.userAgent)) {
experience = 'phone';
} else if (KNOWN_TABLETS.test(navigator.userAgent)) {
experience = 'tablet';
} else if (KNOWN_MOBILE.test(navigator.userAgent)) {
experience = 'mobile';
}
}
// If we already know it's a phone or tablet, we're done here
if (experience && experience != 'mobile') {
render(experience);
return;
}
// Get the screen width
var width = getWidth();
// If we couldn't get the width, but we know we're on a mobile device, serve the phone
// version to be safe. Otherwise, if we don't have a width, just serve the desktop version.
if (!width && experience == 'mobile') {
render('tablet');
return;
} else if (!width) {
render('desktop');
return;
}
// If we know it's mobile, but the screen width is large, don't serve tablet
if (experience == 'mobile' && width > EXPIERIENCES['tablet']['defaultMaxWidth']) {
render('tablet');
return;
}
// If we're here, we either couldn't match the user agent or we know it's mobile,
// but the width is within our max bounds for tablet, so use the width
// to figure out if it's tablet or mobile
if (width <= EXPIERIENCES['phone']['defaultMaxWidth']) {
render('phone');
} else if (width <= EXPIERIENCES['tablet']['defaultMaxWidth']) {
render('tablet');
} else {
render('desktop');
}
}
/**
* Renders the appropriate experience for the user.
* @param {string} experience The appropraite experience(phone,tablet,or desktop)
* @param {?number=} width Screen width
*/
function render(experience, width) {
// Make sure the experience provided is valid
if (!(experience in EXPIERIENCES)) return;
// Remove all old classes from the body
BODY.className = BODY.className.replace(/phone|tablet|desktop/g,'');
// Add this class to the body
BODY.className += ' ' + experience;
// Load additional CSS if necessary
var linkAttr = 'data-href-' + experience,
links, i, src, currentSrc;
links = document.querySelectorAll('link[' + linkAttr + ']');
for (var i=0;i<links.length;i++) {
currentSrc = links[i].getAttribute('href');
src = links[i].getAttribute(linkAttr);
if (src && currentSrc != src) {
links[i].setAttribute('href', src);
}
}
}
// Bind window ready event
window.addEventListener('load', function() {
// Get body tag
BODY = document.getElementsByTagName('body')[0];
resized();
});
// Bind window resize event
window.addEventListener('resize', resized);
// Bind orientation change event for mobile phones
window.addEventListener('orientationchange', resized);
})(window, document, navigator);