Skip to content

Instantly share code, notes, and snippets.

@oosugi20
Created March 15, 2012 07:25
Show Gist options
  • Save oosugi20/2042720 to your computer and use it in GitHub Desktop.
Save oosugi20/2042720 to your computer and use it in GitHub Desktop.
UserAgentから判別して振り分けする
/**
* MY.device
* UserAgentから判別してデバイスを振り分けるためのメソッド
*/
MY.device = (function () {
var _ua = window.navigator.userAgent;
var ua, type;
// iPhone || iPod
if (_ua.indexOf('iPhone') !== -1 || _ua.indexOf('iPod') !== -1) {
ua = 'iPhone';
type = 'smartphone';
}
// iPad
else if (_ua.indexOf('iPad') !== -1) {
ua = 'iPad';
type = 'tablet';
}
// Android
else if (_ua.indexOf('Android') !== -1) {
// Android Mobile
if (_ua.indexOf('Mobile') !== -1) {
ua = 'androidMobile';
type = 'smartphone';
}
// Android Tablet
else {
ua = 'androidTablet';
type = 'tablet';
}
}
// others
else {
ua = 'else';
type = 'else';
}
return {
_ua: ua,
_type: type,
/**
* setClass
* html要素にUserAgentとデバイスのタイプのclass属性を付与
*/
setClass: function () {
$('html')
.addClass('ua-' + this._ua)
.addClass('dt-' + this._type);
}
};
})();
MY.device.setClass();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment