JAVASCRIPT PC MOBILE 구분/ 브라우저 구분


var device = "pc";
var browserName = undefined;
var userAgent = navigator.userAgent.toLowerCase();
var userAgentName = navigator.appName;
var mobileDevice;

var mobilePhones = new Array('iphone','ipod','android','blackberry','windows ce','nokia','webos',

'opera mini','sonyericsson','opera mobi','iemobile');


/* pc, mobile 확인 */

for(var i=0;i<mobilePhones.length;i++) {

if(userAgent.indexOf(mobilePhones[i]) != -1){

device = "mobile";

}

}


/* mobile device name 확인 */

for(var txt in mobilePhones){

    if(userAgent.match(mobilePhones[txt]) != null){

    mobileDevice = mobilePhones[txt];

        break;

    }

}


/* browser name 확인 */

switch (true) {

case /trident|msie/.test(userAgent):

browserName = 'ie';

break;

// IE 12(edge)

case /edge/.test(userAgent):

browserName = 'edge';

break;

case /chrome/.test(userAgent):

browserName = 'chrome';

break;

case /safari/.test(userAgent):

browserName = 'safari';

break;

case /firefox/.test(userAgent):

browserName = 'firefox';

break;

case /opera/.test(userAgent):

browserName = 'opera';

break;

default:

browserName = 'unknown';

}

console.log("browser_name : " + browserName);
console.log("device : " + device);


'Programming > javascript' 카테고리의 다른 글

IOS safari 세션 문제  (0) 2017.01.25
[javascript] 엘리먼트 사이즈 구하기  (0) 2017.01.10
카카오 SDK  (0) 2016.08.19
동영상 video tag  (0) 2016.08.19
페이스북 SDK  (0) 2016.08.19

+ Recent posts