Skip to content

Instantly share code, notes, and snippets.

@yomotsu
Created February 14, 2012 09:13
Show Gist options
  • Save yomotsu/1825024 to your computer and use it in GitHub Desktop.
Save yomotsu/1825024 to your computer and use it in GitHub Desktop.
端末 振り分け, 操作ボタン付き
;(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('&#34920;&#31034;&#20999;&#26367; : <a href="#">PC</a> | &#x30B9;&#x30DE;&#x30DB;&#x7B49;');
$div.find("a").bind("click", function(){goPcPage();});
}else if(!_isMqPage){
$div.html('&#34920;&#31034;&#20999;&#26367; : PC | <a href="#">&#x30B9;&#x30DE;&#x30DB;&#x7B49;</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