2012/04/05

[JavaScript]からOSを判断する

前回、スマホを判定する方法について書きましたが、同様に、PCの判別もできるようです。


var os, ua = navigator.userAgent;
if (ua.match(/Win(dows )?NT 6\.1/)) {
os = "Windows 7"; // Windows 7 の処理
}
else if (ua.match(/Win(dows )?NT 6\.0/)) {
os = "Windows Vista"; // Windows Vista の処理
}
else if (ua.match(/Win(dows )?NT 5\.2/)) {
os = "Windows Server 2003"; // Windows Server 2003 の処理
}
else if (ua.match(/Win(dows )?(NT 5\.1|XP)/)) {
os = "Windows XP"; // Windows XP の処理
}
else if (ua.match(/Win(dows)? (9x 4\.90|ME)/)) {
os = "Windows ME"; // Windows ME の処理
}
else if (ua.match(/Win(dows )?(NT 5\.0|2000)/)) {
os = "Windows 2000"; // Windows 2000 の処理
}
else if (ua.match(/Win(dows )?98/)) {
os = "Windows 98"; // Windows 98 の処理
}
else if (ua.match(/Win(dows )?NT( 4\.0)?/)) {
os = "Windows NT"; // Windows NT の処理
}
else if (ua.match(/Win(dows )?95/)) {
os = "Windows 95"; // Windows 95 の処理
}
else if (ua.match(/Mac|PPC/)) {
os = "Mac OS"; // Macintosh の処理
}
else if (ua.match(/Linux/)) {
os = "Linux"; // Linux の処理
}
else if (ua.match(/^.*\s([A-Za-z]+BSD)/)) {
os = RegExp.$1; // BSD 系の処理
}
else if (ua.match(/SunOS/)) {
os = "Solaris"; // Solaris の処理
}
else {
os = "N/A"; // 上記以外 OS の処理
}

via:OS 別に振り分ける
jQueryのプラグインとして用意するのもありかなーっと思ったり。もしかして、すでにあったり。。。

0 コメント:

コメントを投稿