Skip to content

Instantly share code, notes, and snippets.

@alvin-milton
Created May 12, 2016 17:38
Show Gist options
  • Save alvin-milton/58b65f4306e93b5b8be38eef7dbb1f47 to your computer and use it in GitHub Desktop.
Save alvin-milton/58b65f4306e93b5b8be38eef7dbb1f47 to your computer and use it in GitHub Desktop.
movile redirect
var ismobil,
date = new Date(),
expires,
nameEQ,
ca,
c,
i,
x,
redirectagent,
redirect_devices,
redirect_uri;
searchstring,
mobileregex;
function mobileCreateCookie(name, value, hours) {
if (hours) {
date.setTime(date.getTime() + (24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
} else {
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
}
function mobileReadCookie(name) {
nameEQ = name + "=";
ca = document.cookie.split(';');
for (i = 0; i < ca.length; i++) {
c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
return c.substring(nameEQ.length, c.length);
}
}
return null;
}
function mobileEraseCookie(name) {
mobileCreateCookie(name, "", -1);
}
if (mobileReadCookie('NoMobileRedirect') === null) {
if (/r=off/.test(location) === true) {
mobileEraseCookie('NoMobileRedirect');
mobileCreateCookie('NoMobileRedirect', true, 1);
mobileEraseCookie('isMobile');
mobileCreateCookie('isMobile', true, 1);
} else {
redirectagent = navigator.userAgent.toLowerCase();
redirect_devices = ['alcatel*', 'alltel*', 'amoi*', '*android*', 'armani*', 'asus*', 'au-mic*', 'audio*', 'ax*', 'benq*', 'bird*', 'boost*', '*blackberry*', 'cdm*', 'cocoon*', 'cricket*', '*danger hiptop*', 'docomo*', '*ericsson*', '*ezwap*', 'fly*', '*google wireless transcoder*', 'go.web*', 'gr*', 'gt*', 'haier*', '*helio*', 'hitachi*', 'hpip*', 'htc*', 'i9*', '*iemobile*', 'ipaq*', '*iphone*', '*ipod*', 'kc*', 'kf*', 'kg*', 'k-jam*', 'kwc*', 'kyokx*', 'l1400*', 'lg*', '*lge*', 'lx*', 'm1000*', '*metropcs*', '*midp-2.0*', 'mitsu*', '*mmp/2.0*', 'mm*', 'mobile*', 'mozilla/2.0*', '*mobileexplorer*', 'mot*', 'mp*', 'n7710*', 'n810*', 'nec-*', '*netfront*', 'nok*', '*nokia*', 'o2*', 'obigo*', '*openwave*', 'opwv*', 'palm*', '*palmos*', 'pant*', 'panasonic*', '*palmsource*', 'philips*', 'pg*', 'pl*', 'pm*', 'pn*', 'po*', 'pu*', 'q-*', 'qc*', 'qwest*', '*opera mobi*', '*opera mini*', '*rim handheld*', 'rl*', '*rim pager*', 'sagem*', '*samsung*', 'sch*', 'scp*', 'sgh*', 'sec-s*', 'serenata*', 'sie-*', 'sharp*', '*smartphone*', '*sonyericsson*', 'sph-*', '*sprint*', '*symbianos*', '*symbian os*', '*t-mobile*', '*up.browser*', '*up.link*', 'vodafone*', '*vzw*', '*windows ce*'];
for (x in redirect_devices) {
mobileregex = new RegExp('^' + redirect_devices[x].replace(/\*/g, '.*') + '$');
if (mobileregex.test(redirectagent) === true) {
ismobil = true;
// Not clear on where redirect_uri comes from...
location.replace('http://m.complex.com' + redirect_uri);
// location.replace('http://m.complex.com' + location.pathname);
break;
}
}
if (ismobil !== true) {
mobileEraseCookie('NoMobileRedirect');
mobileCreateCookie('NoMobileRedirect', true, 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment