Created
March 15, 2012 07:25
-
-
Save oosugi20/2042720 to your computer and use it in GitHub Desktop.
UserAgentから判別して振り分けする
This file contains hidden or 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
/** | |
* 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