Skip to content

Instantly share code, notes, and snippets.

@karlcow
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save karlcow/33c369c42df4e3372eda to your computer and use it in GitHub Desktop.

Select an option

Save karlcow/33c369c42df4e3372eda to your computer and use it in GitHub Desktop.
Silly parsing
var userAgent = 'Mozilla/5.0 (Linux; Android 4.4; Nexus 5 Build/BuildID) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36'
userAgent.slice(userAgent.indexOf("Android")+8)
// returns "4.4; Nexus 5 Build/BuildID) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36"
parseFloat(userAgent.slice(userAgent.indexOf("Android")+8))
// returns 4.4
/* DO NOT DO THAT */
/* JS REDIRECT FOR MOBILE */
// GLOBAL SCOPED FUNCTIONS
//New Script Shoptime
function setCookie(name, value) {
document.cookie = name+"="+value+"; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var 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 deleteCookie(name) {
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
function getQueryString() {
var result = {}, queryString = location.search.substring(1),
re = /([^&=]+)=([^&]*)/g, m;
while (m = re.exec(queryString)) {
result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
return result;
}
(function(){
var isAndroid23 = false;
var userAgent = navigator.userAgent;
var isMobile = /iP(hone|od)|Android.*Mobile/.test(userAgent);
var mobileCookie = getCookie("NewMobileOptOut");
if( userAgent.indexOf("Android") >= 0 ) {
var androidversion = parseFloat(userAgent.slice(userAgent.indexOf("Android")+8));
isAndroid23 = androidversion < 2.4;
if (isAndroid23) {
// NEVER TRIGGER A REDIRECT FOR UNSUPPORTED ANDROID DEVICES
return false;
}
}
if (/(windows phone)|(iemobile)/i.test(userAgent)) {
// NEVER REDIRECT ON UNSUPPORTED DEVICES
return false;
}
// Get MobileOptOut from queryString
var mobileQuery = getQueryString();
if (mobileQuery) {
mobileQuery = mobileQuery["MobileOptOut"];
}
// Update MobileOptOut cookie - 0 = delete, 1 = set
if ( mobileQuery == '0' ) {
deleteCookie("NewMobileOptOut");
} else if ( mobileQuery == "1" ) {
setCookie("NewMobileOptOut", mobileQuery);
}
// Redirect
if (isMobile && (mobileCookie != "1" || mobileQuery == "0") && mobileQuery != "1" && mobileQuery != "2") {
window.location = "http://m.shoptime.com.br" + window.location.pathname + window.location.search;
}
// OPT IN LINK
if (isMobile) {
var sitemapLink = document.getElementsByClassName('sitemap-link');
if (sitemapLink && sitemapLink.length > 0) {
sitemapLink[0].innerHTML = sitemapLink[0].innerHTML + "<p><a href=\"#\" onclick=\"setCookie('MobileOptOut', '0'); window.location = window.location.href.replace('MobileOptOut=1',''); return false;\">Acessar a Versao Mobile</a></p>";
}
}
}());
@karlcow
Copy link
Author

karlcow commented Nov 25, 2014

Basically it fails for a UA such as Firefox for Android: Mozilla/5.0 (Android; Mobile; rv:30.0) Gecko/30.0 Firefox/30.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment