FEDev Story

How to detect browser in JavaScript [Chrome, Firefox, Safari, Opera, Edge , MS IE]? 본문

Web.Dev

How to detect browser in JavaScript [Chrome, Firefox, Safari, Opera, Edge , MS IE]?

지구별72 2021. 12. 19. 16:38

https://codepedia.info/detect-browser-in-javascript

 

How to detect browser in JavaScript [Chrome, Firefox, Safari, Opera, Edge ,IE] ?

How to detect browser name in javascript, using navigator.userAgent detect firebox browser in javascript, detect chrome browser , detect IE browser, detect safari browser, detect opera browser, detect edge browser

codepedia.info

 

Approach 1: JavaScript code to detect browser name using userAgent.match

function fnBrowserDetect(){
                 
         let userAgent = navigator.userAgent;
         let browserName;
         
         if(userAgent.match(/chrome|chromium|crios/i)){
             browserName = "chrome";
           }else if(userAgent.match(/firefox|fxios/i)){
             browserName = "firefox";
           }  else if(userAgent.match(/safari/i)){
             browserName = "safari";
           }else if(userAgent.match(/opr\//i)){
             browserName = "opera";
           } else if(userAgent.match(/edg/i)){
             browserName = "edge";
           }else{
             browserName="No browser detection";
           }
         
          document.querySelector("h1").innerText="You are using "+ browserName +" browser";         
  }

Approach 2: JavaScript code to detect browser using userAgent.IndexOf

var browserName = (function (agent) {        
	switch (true) {
            case agent.indexOf("edge") > -1: return "MS Edge";
            case agent.indexOf("edg/") > -1: return "Edge ( chromium based)";
            case agent.indexOf("opr") > -1 && !!window.opr: return "Opera";
            case agent.indexOf("chrome") > -1 && !!window.chrome: return "Chrome";
            case agent.indexOf("trident") > -1: return "MS IE";
            case agent.indexOf("firefox") > -1: return "Mozilla Firefox";
            case agent.indexOf("safari") > -1: return "Safari";
            default: return "other";
        }
    })(window.navigator.userAgent.toLowerCase());

document.querySelector("h1").innerText="You are using "+ browserName +" browser";

 

'Web.Dev' 카테고리의 다른 글

video 넣는 방법  (0) 2021.12.23
playsinline - iOS비디오정책  (0) 2021.12.23
How to hide HTML5 video controls completely  (0) 2021.12.16
탑 스크롤 애니메이션 구현하기  (0) 2021.09.09
페이지 로딩 속도 측정  (0) 2021.08.27
Comments