Last active
August 29, 2015 14:01
-
-
Save Nully/913742e73228d2069edc to your computer and use it in GitHub Desktop.
UAからモバイル判別
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
(function($){ | |
var $window = $(window), | |
ua = navigator.userAgent.toLowerCase(); | |
$.mbua = $.mbUa = $.mbUA = { | |
/** | |
* Android端末でモバイル扱いしたい端末のUAの文字列の一部を格納する | |
* この配列に入っている端末(UAの文字列)はメソッドから追加することができる | |
* | |
* @access protected | |
* @var array | |
*/ | |
_androidMobileUAs: [], | |
/** | |
* 画面幅が640px以下であればtrueを返却 | |
* 画面幅はCSSのブレイクポイントの幅 | |
* | |
* @access public | |
* @return bool | |
*/ | |
isMobileWindowSize: function() { | |
return $window.width() <= 640; | |
}, | |
/** | |
* Androidでモバイル扱いしたいUAを追加する | |
* | |
* @access public | |
* @var array | |
*/ | |
pushAndroidUa: function(uas) { | |
var _u = ($.isArray(uas) ? uas: [uas]), t, self = this; | |
$.each(_u, function(i, v){ | |
self._androidMobileUAs.push(v.toLowerCase()); | |
}); | |
return this; | |
}, | |
/** | |
* Android端末かどうかをUAから調べる | |
* _androidMobileUAsに入っている文字列もあわせて判定する | |
* | |
* @access public | |
* @return bool | |
*/ | |
isAndroid: function() { | |
var _test = false; | |
$.each(this._androidMobileUAs, function(i, v){ | |
if(new RegExp(v).test(ua)) { | |
_test = true; | |
return false; | |
} | |
}); | |
if(_test == false) { | |
_test = new RegExp("android(.*?)mobile").test(ua); | |
} | |
return _test; | |
}, | |
/** | |
* iOSデバイスかを調べる | |
* | |
* @access public | |
* @return bool | |
*/ | |
isIOS: function() { | |
return /like mac(.*?)mobile(.*?)safari/.test(ua); | |
}, | |
/** | |
* iPhoneかどうかを調べる | |
* | |
* @access public | |
* @return bool | |
*/ | |
isIPhone: function() { | |
return /iphone/.test(ua); | |
}, | |
/** | |
* iPadかどうかを調べる | |
* | |
* @access public | |
* @return bool | |
*/ | |
isIPad: function() { | |
return /ipad/.test(ua); | |
}, | |
/** | |
* iPodかどうかを調べる | |
* | |
* @access public | |
* @return bool | |
*/ | |
isIPod: function() { | |
return /ipod/.test(ua); | |
}, | |
/** | |
* バージョン番号を取得する | |
* 取得出来るバージョンは | |
* メジャー | |
* マイナー | |
* ビルド(あれば) | |
* が取得できる | |
* | |
* @access public | |
* @return object | |
*/ | |
getVersion: function() { | |
var _v = { | |
major: null, | |
minor: null, | |
build: null | |
}, reg, rep; | |
// デバイスにあわせてRegExpオブジェクトを振り分ける | |
if(this.isAndroid()) { | |
reg = /android\s(\d)(.\d)?(\.\d)?/; | |
rep = "."; | |
} | |
else if(this.isIOS()) { | |
reg = /os (\d)_(\d)(_\d)?/; | |
rep = "_"; | |
} | |
else { | |
return _v; | |
} | |
var versions = ua.match(reg); | |
if(versions[1] != undefined) { | |
_v.major = parseInt(versions[1]); | |
} | |
if(versions[2] != undefined) { | |
_v.minor = parseInt(versions[2].replace(rep, "")); | |
} | |
if(versions[3] != undefined) { | |
_v.build = parseInt(versions[3].replace(rep, "")); | |
} | |
return _v; | |
} | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment