forked from sebarmeli/JS-Redirection-Mobile-Site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirection_mobile_testable.js
executable file
·193 lines (150 loc) · 6.88 KB
/
redirection_mobile_testable.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*!
* JS Redirection Mobile
*
* Copyright (c) 2011 Sebastiano Armeli-Battana (http://sebarmeli.com)
* Dual licensed under the MIT or GPL Version 3 licenses.
* @link http://github.com/sebarmeli/JS-Redirection-Mobile-Site/
*/
/*
* This version is exactly the same as redirection_mobile.js, but uses few arguments, such as "document",
* "window", "navigator" for testing purpose.
*
* @author Sebastiano Armeli-Battana
* @version 0.9.5
*
*/
/*globals window,document, navigator, SA */
if (!window.SA) {window.SA = {};}
/*
* @param configuration object containing three properties:
* - mobile_prefix : prefix appended to the hostname (such as "m" to redirect to "m.domain.com")
* - mobile_url : mobile url to use for the redirection (such as "mobile.whatever.com" to redirect to "whatever.com" )
* - mobile_scheme : url scheme (http/https) of the mobile site domain
* - cookie_hours : number of hours the cookie needs to exist after redirection to desktop site
* - noredirection_param : parameter to pass in the querystring of the URL to avoid the redirection (the value must be equal to "true").
* It's also the name of the item in the localStorage (or cookie name) to avoid mobile
* redirection. Default value is "noredirection". Eg: http://domain.com?noredirection=true
* - tablet_redirection : boolean value that enables/disables(default) the redirection for tablet such as iPad, Samsung Galaxy Tab, Kindle or Motorola Xoom. - Default:false
* Default Url for redirection will be the same as the one for mobile devices.
* - tablet_url : url to use for the redirection in case user is using a tablet to access the site
* - keep_path : boolean to determine if the destination url needs to keep the path from the original url
* - keep_query : boolean to determine if the destination url needs to keep the querystring from the original url
* - beforeredirection_callback : callback launched before the redirection happens
*
*/
SA.redirection_mobile = function(document, window, navigator, configuration) {
// Helper function for adding time to the current date
var addTimeToDate = function(msec) {
// Get the current date
var exdate = new Date();
// Add time to the date
exdate.setTime(exdate.getTime() + msec);
//Return the new Date
return exdate;
};
// Helper function for getting a value from a parameter in the querystring
var getQueryValue = function(param) {
if (!param) {
return;
}
var querystring = document.location.search,
queryStringArray = querystring && querystring.substring(1).split("&"),
i = 0,
length = queryStringArray.length;
for (; i < length; i++) {
var token = queryStringArray[i],
firstPart = token && token.substring(0, token.indexOf("="));
if (firstPart === param ) {
return token.substring(token.indexOf("=") + 1, token.length);
}
}
};
var agent = navigator.userAgent.toLowerCase(),
// Constants
FALSE = "false",
TRUE = "true",
// configuration object
config = configuration || {},
// parameter to pass in the URL to avoid the redirection
redirection_param = config.noredirection_param || "noredirection",
//cookie name for referrer
referrer_cookie_name = "_referrer",
// prefix appended to the hostname
mobile_prefix = config.mobile_prefix || "m",
// new url for the mobile site domain
mobile_url = config.mobile_url,
// protocol for the mobile site domain
mobile_protocol = config.mobile_scheme ?
config.mobile_scheme + ":" :
document.location.protocol,
// URL host of incoming request
host = document.location.host,
// value for the parameter passed in the URL to avoid the redirection
queryValue = getQueryValue(redirection_param),
// Compose the mobile hostname considering "mobile_url" or "mobile_prefix" + hostname
mobile_host = mobile_url ||
(mobile_prefix + "." +
(!!host.match(/^www\./i) ?
host.substring(4) :
host)),
// Expiry hours for cookie
cookie_hours = config.cookie_hours || 1,
// Parameters to determine if the pathname and the querystring need to be kept
keep_path = config.keep_path || false,
keep_query = config.keep_query || false,
// new url for the tablet site domain
tablet_host = config.tablet_url || mobile_host,
// Check if the UA is a mobile one (iphone, ipod, android, blackberry)
isUAMobile =!!(agent.match(/(iPhone|iPod|blackberry|android 0.5|htc|lg|midp|mmp|mobile|nokia|opera mini|palm|pocket|psp|sgh|smartphone|symbian|treo mini|Playstation Portable|SonyEricsson|Samsung|MobileExplorer|PalmSource|Benq|Windows Phone|Windows Mobile|IEMobile|Windows CE|Nintendo Wii)/i));
// Check if the referrer was a mobile page (probably the user clicked "Go to full site") or in the
// querystring there is a parameter to avoid the redirection such as "?mobile_redirect=false"
// (in that case we need to set a variable in the sessionStorage or in the cookie)
if (document.referrer.indexOf(mobile_host) >= 0 || queryValue === TRUE ) {
if (window.sessionStorage) {
window.sessionStorage.setItem(redirection_param, TRUE);
} else {
document.cookie = redirection_param + "=" + TRUE + ";expires="+
addTimeToDate(3600*1000*cookie_hours).toUTCString();
}
}
// Check if the sessionStorage contain the parameter
var isSessionStorage = (window.sessionStorage) ?
(window.sessionStorage.getItem(redirection_param) === TRUE) :
false,
// Check if the Cookie has been set up
isCookieSet = document.cookie ?
(document.cookie.indexOf(redirection_param) >= 0) :
false;
// Check if the device is a Tablet such as iPad, Samsung Tab, Motorola Xoom or Amazon Kindle
if (!!(agent.match(/(iPad|SCH-I800|xoom|kindle)/i))) {
// Check if the redirection needs to happen for iPad
var isUATablet = (config.tablet_redirection === TRUE || !!config.tablet_url) ? true : false;
isUAMobile = false;
}
// Check that User Agent is mobile, cookie is not set or value in the sessionStorage not present
if ((!!isUATablet || !!isUAMobile) && !(isCookieSet || isSessionStorage)) {
// Callback call
if (config.beforeredirection_callback) {
if (!config.beforeredirection_callback.call(this)) {
return;
}
}
var path_query = "";
if(keep_path) {
path_query += document.location.pathname;
}
if (keep_query) {
path_query += document.location.search;
}
// copy the referrer url to a cookie so it's not lost on redirection
if (document.referrer) {
document.cookie = referrer_cookie_name + "=" + document.referrer + ";expires="+
addTimeToDate(3600*1000*cookie_hours).toUTCString();
}
if (isUATablet){
document.location.href = mobile_protocol + "//" + tablet_host + path_query;
} else if (isUAMobile) {
document.location.href = mobile_protocol + "//" + mobile_host + path_query;
}
}
};