JavaScriptでブラウザとバージョンを取得する

JavaScript

JavaScriptを使ってブラウザとそのバージョンを取得する関数を作成しましたので紹介します。

基本的なことですが、ユーザーエージェントからブラウザ情報を得ます。

ユーザーエージェント

■Windows
▼Google Chrome
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36

▼Firefox
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0

▼Microsoft Edge
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18362

▼Microsoft Edge(Chromium)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Edg/83.0.478.54

▼Opera(Chromium)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 OPR/68.0.3618.173

▼Internet Exploer
Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Zoom 3.6.0; rv:11.0) like Gecko

■iPhone
▼Safari
Mozilla/5.0 (iPhone; CPU iPhone OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Mobile/15E148 Safari/604.1

▼Firefox
Mozilla/5.0 (iPhone; CPU OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/26.0 Mobile/15E148 Safari/605.1.15

▼Google Chrome
Mozilla/5.0 (iPhone; CPU iPhone OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/83.0.4103.88 Mobile/15E148 Safari/604.1

▼Opera
Mozilla/5.0 (iPhone; CPU iPhone OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) OPT/2.4.1 Mobile/15E148

■Android
▼Google Chrome
Mozilla/5.0 (Linux; Android 9; SH-01K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Mobile Safari/537.36

▼Microsoft Edge
Mozilla/5.0 (Linux; Android 9; SH-01K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.116 Mobile Safari/537.36 EdgA/45.05.4.5026

▼Opera
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 Edg/81.0.416.81

▼Yahoo
Mozilla/5.0 (Linux; Android 9; SH-01K Build/S2170; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/83.0.4103.106 Mobile Safari/537.36 YJApp-ANDROID jp.co.yahoo.android.yjtop/3.69.1

▼SamsungBrowser
Mozilla/5.0 (Linux; Android 9; SH-01K) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/11.2 Chrome/75.0.3770.143 Mobile Safari/537.36

■Mac
▼Safari
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15

▼Google Chrome
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36

iPhone(iPad)だけ、ChromeやFirefox、Microsoft Edgeなどのユーザエージェントが独自のようです

ブラウザ判定関数

var result = getBrowserInfo();

document.write(result);

function getBrowserInfo(){
  var ua = window.navigator.userAgent;

  var uaStr = ua + ";";
  uaStr = uaStr.replace(/ /g, ";");

  var browser = "";

  if (isBrowser(uaStr, "MSIE") ){
    //古いInternet Explorer
    browser = "Internet Explorer Ver." + getVersion(uaStr, "MSIE;");
  }
  else if (isBrowser(uaStr, "Trident")){
    //Internet Explorer
    browser = "Internet Explorer Ver.11";
  }
  else if (isBrowser(uaStr, "Edge")){
    //古いMicrosoft Edge
    browser = "Microsoft Edge Ver." + getVersion(uaStr, "Edge/");
  }
  else if (isBrowser(uaStr, "EdgiOS")){
    //iPhoneのMicrosoft Edge
    browser = "Microsoft Edge Ver." + getVersion(uaStr, "EdgiOS/");
  }
  else if (isBrowser(uaStr, "Opera/")){
    //古いOpera
    browser = "Opera Ver." + getVersion(uaStr, "Opera/");
  }
  else if (isBrowser(uaStr, "OPT")){
    //iPhoneのOpera
    browser = "Opera Ver." + getVersion(uaStr, "OPT/");
  }
  else if (isBrowser(uaStr, "Firefox")){
    //Firefox
    browser = "Firefox Ver." + getVersion(uaStr, "Firefox/");
  }
  else if (isBrowser(uaStr, "FxiOS")){
    //iPhoneのFirefox
    browser = "Firefox " + getVersion(uaStr, "FxiOS/");
  }
  else if (isBrowser(uaStr, "Chrome")){
    //Google Chrome、Chromium(新しいMicrosoft Edge、新しいOpera、Samsung、Yahooなど)
    browser = "Google Chrome Ver." + getVersion(uaStr, "Chrome/");
  }
  else if (isBrowser(uaStr, "CriOS")){
    //iPhoneのGoogle Chrome
    browser = "Google Chrome Ver." + getVersion(uaStr, "CriOS/");
  }
  else if (isBrowser(uaStr, "Safari") && isBrowser(uaStr, "Version") ){
    //Safari
    browser = "Safari Ver." + getVersion(uaStr, "Version/");
  }
  else{
    browser = "その他";
  }

  return browser;
}

function getVersion(uaStr, searchStr){
  startStr = uaStr.indexOf(searchStr) + searchStr.length;
  endStr   = uaStr.indexOf(";", startStr);

  var version = uaStr.substring(startStr, endStr);

  return version;
}

function isBrowser(uaStr, searchStr){
  if(uaStr.indexOf(searchStr)>=0){
    return true;
  }
  else{
    return false;
  }
}
Chromiumのブラウザは、Chromeとして判定されます。
Microsoft Edge、Opera、SamsungBrowser、Yahooなど