Created
February 14, 2012 09:13
-
-
Save yomotsu/1825024 to your computer and use it in GitHub Desktop.
端末 振り分け, 操作ボタン付き
This file contains 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
;(function($){ | |
var _isMobile = (function() { | |
var useragents = [ | |
'iPhone', // Apple iPhone | |
'iPod', // Apple iPod touch | |
'iPad', // Apple iPad | |
'Android', // Android | |
'Windows Phone OS', // WindowsPhone | |
'BlackBerry', // BlackBerry | |
'webOS', // Palm Pre Experimental | |
'PlayStation Vita' // PlayStation Vita | |
]; | |
var pattern = new RegExp(useragents.join('|'), 'i'); | |
return pattern.test(navigator.userAgent); | |
})(); | |
var _isMqPage = (function() { | |
if(/^\/mw\//.test(location.pathname)){ | |
return true; | |
} | |
return false; | |
})(); | |
var _isModernBrowser = (function() { | |
if(!!document.uniqueID){ | |
if(!!window.localStorage){ | |
return true; //ie8+ | |
}else{ | |
return false; //ie6,7 | |
} | |
}else{ | |
return true; //!ie | |
} | |
}); | |
var cookie = { | |
name:"viewState", | |
limitDay:365, | |
set:function(data){ | |
var today = new Date(); | |
today.setTime(today.getTime() + (1000 * 60 * 60 * 24 * Number(cookie.limitDay))); | |
document.cookie = cookie.name + '=' + encodeURIComponent(data) + '; path=/; expires=' + today.toGMTString(); | |
}, | |
get:function(){ | |
var data; | |
return (data = ('; ' + document.cookie + ';').match('; ' + cookie.name + '=(.*?);')) ? decodeURIComponent(data[1]) : null; | |
} | |
} | |
if(_isModernBrowser){ | |
if(_isMobile && !_isMqPage && !/^(mw|pc)$/.test(cookie.get())){ | |
goMqPage(); | |
} | |
// else if(!_isMobile && _isMqPage && !/^(mw|pc)$/.test(cookie.get())){ | |
// goPcPage(); | |
// } | |
else if(!_isMqPage && /^mw$/.test(cookie.get())){ | |
goMqPage(); | |
} | |
else if(_isMqPage && /^pc$/.test(cookie.get())){ | |
goPcPage(); | |
} | |
}else if(_isMqPage){ | |
goPcPage(); | |
} | |
// misc functions | |
function goMqPage(e){ | |
if(e){ | |
e.preventDefault(); | |
} | |
var mqPage = "/mw" + location.pathname; | |
location.replace(mqPage); | |
cookie.set("mw"); | |
} | |
function goPcPage(e){ | |
if(e){ | |
e.preventDefault(); | |
} | |
var pcPage = location.pathname.replace(/^\/mw/, ''); | |
location.replace(pcPage); | |
cookie.set("pc"); | |
} | |
//append html to div | |
if(_isModernBrowser){ | |
$(function(){ | |
var $div = $(".mod-deviceOptimum"); | |
if(_isMqPage){ | |
$div.html('表示切替 : <a href="#">PC</a> | スマホ等'); | |
$div.find("a").bind("click", function(){goPcPage();}); | |
}else if(!_isMqPage){ | |
$div.html('表示切替 : PC | <a href="#">スマホ等</a>'); | |
$div.find("a").bind("click", function(){goMqPage();}); | |
} | |
}); | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment