ブラウザ判定

userAgentによる判定

Navigator.userAgentの値から判定する方法です。

ライブラリによる実装

デバイスがiPhoneであるかの判定は、各ライブラリでは以下のように実装されています。

device.js

GitHub - matthewhudson/device.js: Device.js makes it easy to write conditional CSS _and/or_ JavaScript…
device = {};

// The client user agent string.
// Lowercase, so we can use the more efficient indexOf(), instead of Regex
userAgent = window.navigator.userAgent.toLowerCase();


device.iphone = function () {
    return !device.windows() && find('iphone');
};

device.windows = function () {
    return find('windows');
};


// Simple UA string search
find = function (needle) {
    return userAgent.indexOf(needle) !== -1;
};
device.js/device.js at master · matthewhudson/device.js · GitHub

is.js

GitHub - arasatasaygin/is.js: Micro check library
// store navigator properties to use later
var userAgent = 'navigator' in window && 'userAgent' in navigator && navigator.userAgent.toLowerCase() || '';

// is current device iphone?
is.iphone = function() {
    return /iphone/i.test(userAgent);
is.js/is.js at master · arasatasaygin/is.js · GitHub

detectmobilebrowser.js

Detect Mobile Browsers - Open source mobile phone detection
(function(a,b)
{
    if(/ … |ip(hone|od)| … /i.test(a.substr(0,4)))
    …
})(navigator.userAgent||navigator.vendor||window.opera, … );
JavaScriptのドキュメントから検索