Javascript Browser Navigator userAgent Property get Browser Name

Description

Javascript Browser Navigator userAgent Property get Browser Name

View in separate window

  <!DOCTYPE html> 
  <html lang="en"> 
  <body> 
      <script> 
          function getBrowserName() { 
              let lsBrowser = navigator.userAgent; 
              if (lsBrowser.indexOf("MSIE") >= 0) { 
                  return "MSIE"; 
              } else if (lsBrowser.indexOf("Firefox") >= 0) { 
                  return "Firefox"; 
              } else if (lsBrowser.indexOf("Chrome") >= 0) { 
                  return "Chrome"; 
              } else if (lsBrowser.indexOf("Safari") >= 0) { 
                  return "Safari"; 
              } else if (lsBrowser.indexOf("Opera") >= 0) { 
                  return "Opera"; 
              } else { /*w  ww . jav a 2 s. co m*/
                  return "UNKNOWN"; 
              } 
          } 
          function getBrowserVersion() { 
              let ua = navigator.userAgent; 
              let browser = getBrowserName(); 
              let findIndex = ua.indexOf(browser) + browser.length + 1; 
              let browserVersion = parseFloat( 
                  ua.substring(findIndex, findIndex + 3)); 
              return browserVersion; 
          } 
        let browserName = getBrowserName(); 
        let browserVersion = getBrowserVersion(); 
        if (browserName == "MSIE") { 
            if (browserVersion < 9) { 
                console.log("Your version of IE is too old"); 
            } else { 
                console.log("Your version of IE is fully  supported"); 
            } 
        } else if (browserName == "Firefox") { 
            console.log("Firefox is fully supported"); 
        } else if (browserName == "Safari") { 
            console.log("Safari is fully supported"); 
        } else if (browserName == "Chrome") { 
            console.log("Chrome is fully supported"); 
        } else if (browserName == "Opera") { 
            console.log("Opera is fully supported"); 
        } else { 
            console.log("Sorry this browser version is not supported"); 
        } 
    </script> 
</body> 
</html> 



PreviousNext

Related