Skip to content

Instantly share code, notes, and snippets.

@grammaticof
Created September 5, 2012 11:22
Show Gist options
  • Save grammaticof/3635239 to your computer and use it in GitHub Desktop.
Save grammaticof/3635239 to your computer and use it in GitHub Desktop.
JS - Mobile redirect
function mobile_redirect() {
var path = window.location.pathname;
if (path != null) {
window.location = 'http://host' + path
} else {
window.location = 'http://host/'
}
}
function setCookie(c_name, value, expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";path=/;expires=" + exdate.toUTCString())
}
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) c_end = document.cookie.length;
return unescape(document.cookie.substring(c_start, c_end))
}
}
return ""
}
function gup(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null) return "";
else return results[1]
}
var mobile_fullsite = gup('fullsite');
if (mobile_fullsite == 'true') {
setCookie('mobile_fullsite', '1', '365')
}
var mobile_cookie = getCookie('mobile_fullsite');
if (mobile_cookie == null || mobile_cookie == "") {
var agent = navigator.userAgent;
var agents = [];
agents[0] = 'Sprint';
agents[1] = 'BlackBerry';
agents[2] = 'iPhone';
agents[3] = 'iPod';
agents[4] = 'Android';
agents[5] = 'BlackBerry';
agents[6] = 'HTC';
agents[7] = 'LG';
agents[8] = 'MOT-';
agents[9] = 'Nokia';
agents[10] = 'SymbianOS';
agents[11] = 'Palm';
agents[12] = 'webOS';
agents[13] = 'SAMSUNG';
agents[14] = 'Opera Mini';
agents[15] = 'SonyEricsson';
agents[16] = 'Sanyo';
for (i = 0; i < agents.length; i++) {
var cur_agent = agents[i];
if (agent.match(cur_agent)) {
mobile_redirect()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment