Skip to content

Instantly share code, notes, and snippets.

@parkr
Last active December 20, 2015 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parkr/6123221 to your computer and use it in GitHub Desktop.
Save parkr/6123221 to your computer and use it in GitHub Desktop.
This is what cornell.edu uses to redirect people to the mobile site. Lord. Have. Mercy.
// Redirect to m.cornell.edu for smart phones.
// (We allow an override of the redirect with a certain parameter.)
var CUhomepage = {};
CUhomepage.ua = navigator.userAgent.toLowerCase();
CUhomepage.params = location.search.toLowerCase();
CUhomepage.mobileURL = "http://m.cornell.edu";
CUhomepage.goMobile = false;
// Detect iPhone and iTouch.
if ((CUhomepage.ua.indexOf("iphone") > -1) || (CUhomepage.ua.indexOf("itouch") > -1)) {
CUhomepage.goMobile = true;
}
// Detect Android phone (and not tablet; only works if tablet removes "mobile" from user agent string.
if ((CUhomepage.ua.indexOf("android") > -1) && (CUhomepage.ua.indexOf("mobile") > -1)) {
CUhomepage.goMobile = true;
}
// Override the redirect via parameter specification.
if (CUhomepage.params.indexOf("overridemobile") > -1) {
CUhomepage.goMobile = false;
}
if (CUhomepage.goMobile) {
window.location = CUhomepage.mobileURL;
}
var Redirector = {
redirect: function(){
window.location = "http://m.cornell.edu";
},
present: function(str, search){
return str.indexOf(search) > -1;
}
notOverridden: function(params){
return Redirector.present(params, "overridemobile");
},
isIphone: function(agent){
return Redirector.present(params, "iphone") ||
Redirector.present(params, "itouch");
},
isAndroid: function(agent){
return Redirector.present(params, "android") ||
Redirector.present(params, "mobile");
},
isMobile: function(agent){
return Redirector.isIphone(agent) || Redirector.isAndroid(agent);
},
shouldSwitchToMobile: function(agent, params){
return Redirector.isMobile(agent) && Redirector.notOverridden(params);
}
};
if(Redirector.shouldSwitchToMobile(
navigator.userAgent.toLowerCase(),
location.search.toLowerCase()
)) {
Redirector.redirect();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment