Last active
August 29, 2015 14:23
-
-
Save Cartman0/0aa29acafa16787b13c1 to your computer and use it in GitHub Desktop.
UserAgent を用いて端末判別
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
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>UserAgent Test</title> | |
<meta name="description" content=""> | |
<style> | |
@import url(http://fonts.googleapis.com/earlyaccess/notosansjapanese.css); | |
html, body { | |
background-color: #f0f0f0; | |
font-size: 14px; | |
font-family: 'Noto Sans Japanese', sans-serif; | |
text-align: center; | |
} | |
#device { | |
color: red; | |
font-size: 2rem; | |
} | |
#mobile { | |
color: blue; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>端末判別テスト</h1> | |
<p>この端末は、<span id="device"></span><span id="mobile"></span> です。</p> | |
<script> | |
(function(){ | |
console.log(window.navigator.userAgent); | |
var _ua = (function(u){ | |
var mobile = { | |
0: (u.indexOf("windows") != -1 && u.indexOf("phone") != -1) | |
|| u.indexOf("iphone") != -1 | |
|| u.indexOf("ipod") != -1 | |
|| (u.indexOf("android") != -1 && u.indexOf("mobile") != -1) | |
|| (u.indexOf("firefox") != -1 && u.indexOf("mobile") != -1) | |
|| u.indexOf("blackberry") != -1, | |
iPhone: (u.indexOf("iphone") != -1), | |
Android: (u.indexOf("android") != -1 && u.indexOf("mobile") != -1) | |
}; | |
var tablet = (u.indexOf("windows") != -1 && u.indexOf("touch") != -1) | |
|| u.indexOf("ipad") != -1 | |
|| (u.indexOf("android") != -1 && u.indexOf("mobile") == -1) | |
|| (u.indexOf("firefox") != -1 && u.indexOf("tablet") != -1) | |
|| u.indexOf("kindle") != -1 | |
|| u.indexOf("silk") != -1 | |
|| u.indexOf("playbook") != -1; | |
var pc = !mobile[0] && !tablet; | |
return { | |
Mobile: mobile, | |
Tablet: tablet, | |
PC: pc | |
}; | |
})(window.navigator.userAgent.toLowerCase()); | |
// デバイス検出 | |
var searchDevice = (function(ua){ | |
if(ua.Mobile[0]){ | |
return Object.keys(ua)[0] | |
}else if(ua.Tablet){ | |
return Object.keys(ua)[1]; | |
}else{ | |
return Object.keys(ua)[2]; | |
} | |
})(_ua); | |
var device = document.getElementById('device'); | |
device.innerHTML = searchDevice; | |
// モバイル検出 | |
if(_ua.Mobile[0]){ | |
var searchMobile = (function(ua){ | |
var st = '('; | |
var ed = ')'; | |
for(var j = 1; j < Object.keys(ua.Mobile).length; j++){ | |
var key = Object.keys(ua.Mobile)[j]; | |
if(ua.Mobile[key]){ | |
return st + Object.keys(ua.Mobile)[j] + ed; | |
} | |
} | |
return st + Object.keys(ua.Mobile).slice(1, (Object.keys(ua.Mobile).length)-1) + '以外' + ed; | |
})(_ua); | |
var mobile = document.getElementById('mobile'); | |
mobile.innerHTML = searchMobile; | |
} | |
})(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment