Skip to content

Instantly share code, notes, and snippets.

@hideki-a
Last active January 2, 2016 19:09
Show Gist options
  • Save hideki-a/8348165 to your computer and use it in GitHub Desktop.
Save hideki-a/8348165 to your computer and use it in GitHub Desktop.
(function ($) {
if (SDJL.mobile.smartphone) {
$(function () {
$("body").prepend("<p class='mod-changeview'><a href='/sp/'>スマートフォンサイトを閲覧</a></p>");
});
}
}(jQuery));
.mod-changeview {
margin-bottom: 0;
padding: 10px 0;
background-color: #ccc;
text-align: center;
a {
display: inline-block;
position: relative;
padding: 15px 40px 15px 90px;
border: 1px solid #999;
border-radius: 3px;
background: #fefefe url(/common/images/icon_sp.png) no-repeat 40px center;
color: #333333;
font-size: 3.71429em;
text-decoration: none;
}
}
var SDJL;
if(!SDJL) {
SDJL = {};
} else if(typeof SDJL !== "object") {
throw new Error("Namespace 'SDJL' already exists.");
}
// ------------------------
// User-Agent簡易判定
// (Ver. 2.0)
// ------------------------
SDJL.mobile = (function() {
var ua = navigator.userAgent;
var ret = {
ios : false,
android : false,
windows : false,
smartphone : false
};
if(ua.indexOf("iPhone") > 0 || ua.indexOf("iPad") > 0 || ua.indexOf("iPod") > 0) {
// iOS搭載端末
ret.ios = true;
if(ua.indexOf("iPad") === -1) {
ret.smartphone = true;
}
} else if(ua.indexOf("Android") > 0) {
// Android搭載端末
ret.android = true;
if(ua.indexOf("Mobi") > 0) {
ret.smartphone = true;
}
} else if(ua.indexOf("Windows Phone") > 0) {
// Windows搭載端末
ret.windows = true;
ret.smartphone = true;
}
return ret;
}());
(function () {
if (!SDJL.mobile.smartphone) {
return;
}
if (sessionStorage.viewmode === undefined) {
sessionStorage.viewmode = "";
}
if (sessionStorage.viewmode === "") {
if( confirm( 'このサイトにはスマートフォン用のサイトがあります。\n表示しますか?' ) ) {
sessionStorage.viewmode = "sp";
location.href = '/sp/';
} else {
sessionStorage.viewmode = "pc";
}
}
}());
var movePCSiteBtn = document.querySelector(".js-pcsite");
function movePCSite(e) {
var xhr = new XMLHttpRequest(),
url = location.href,
pcUrl = url.replace("/sp/", "/");
e.preventDefault();
xhr.open("HEAD", pcUrl, true);
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.status === 200) {
sessionStorage.viewmode = "pc";
location.href = pcUrl;
} else {
location.href = "/?viewmode=pc";
}
}
}
movePCSiteBtn.addEventListener("click", movePCSite, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment