본문 바로가기

카테고리 없음

[javascript] os 구분

현재 나는 webview로 앱을 운영하는 회사에 있다.


ios와 android 두개의 os에서 동작하는 함수명이 다르기 때문에 구분을 줘야한다. 


이번에는 js 단에서 os를 구분하는 코드를 남기려한다.


var currentOS;
var mobile = (/iphone|ipad|ipod|android/i.test(navigator.userAgent.toLowerCase()));

if (mobile) {
// 유저에이전트를 불러와서 OS를 구분합니다.
var userAgent = navigator.userAgent.toLowerCase();
if (userAgent.search("android") > -1)
currentOS = "android";
else if ((userAgent.search("iphone") > -1) || (userAgent.search("ipod") > -1)
|| (userAgent.search("ipad") > -1))
currentOS = "ios";

else
currentOS = "else";
} else {
// 모바일이 아닐 때
currentOS = "nomobile";
}