브라우저 종류 체크 on redgoose note

브라우저 종류 체크

Nest: Development Category: Javascript 2019-06-30

브라우저 종류를 체크하는 자바스크립트

var userAgent = navigator.userAgent.toLowerCase();
var browser = {
    msie    : /msie/.test( userAgent ) && !/opera/.test( userAgent ),
    safari  : /webkit/.test( userAgent ),
    firefox : /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent ),
    opera   : /opera/.test( userAgent )
};

if( browser.msie ){ //IE
    console.log("IE")
} else if ( browser.safari ){ //Chrome || Safari
    console.log("Chrome Safari");
} else if ( browser.opera ){ // Opera
    console.log("opera");
} else { 
    console.log("etc");
}