| Viewed 97 times

How I can Detect Browser from User Agent in JavaScript?


I need a function that takes a string (browser identifier) and returns the browser name. Examples detectBrowser("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36") // "Google Chrome"

1 Answer
Mohammad Qandeel
Answered
20

This function  detect browser from User-Agent

function detectBrowser(userAgent){
 if (userAgent.includes("Firefox")) {
  return "Mozilla Firefox";
 } else if (userAgent.includes("Chrome")) {
  return "Google Chrome";
 } else {
  return "Internet Explorer";
 }
}

Example

detectBrowser(navigator.userAgent) // "Google Chrome"



Related Questions