Last active
December 20, 2015 11:28
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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