JavaScriptnavigator.appName 瀏覽器偵測 - JavaScript

瀏覽器偵測 navigator.appName 編碼名稱

Internet Explorer 11 修改了瀏覽器的 user agent,原來的識別碼「MSIE」不見了,參考 使用者代理字串變更

例如以 Windows 10IE11 顯示

Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0;... rv:11.0) like Gecko

navigator.userAgent

rv 來判斷 IE11的版本。

var rv;
var ua = navigator.userAgent;
if (new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})").exec(ua) != null) {
  rv = parseFloat(RegExp.$1);
} else {
  rv = -1;
}

瀏覽器檢測 JavaScript Browser Detection

var gAgent = navigator.userAgent;
var gName = navigator.appName;
var fullVer = "" + parseFloat(navigator.appVersion);
var majorVer = parseInt(navigator.appVersion, 10);
var sName, sVer, sTrim;
if ((sVer = gAgent.indexOf("Opera")) != -1) {
  gName = "Opera";
  fullVer = gAgent.substring(sVer + 6);
  if ((sVer = gAgent.indexOf("Version")) != -1) fullVer = gAgent.substring(sVer + 8);
} else if ((sVer = gAgent.indexOf("MSIE")) != -1) {
  gName = "Microsoft Internet Explorer";
  fullVer = gAgent.substring(sVer + 5);
} else if ((sVer = gAgent.indexOf("Sleipnir")) != -1) {
  gName = "Sleipnir";
  fullVer = gAgent.substring(sVer + 9);
} else if ((sVer = gAgent.indexOf("Chrome")) != -1) {
  gName = "Chrome";
  fullVer = gAgent.substring(sVer + 7);
} else if ((sVer = gAgent.indexOf("Safari")) != -1) {
  gName = "Safari";
  fullVer = gAgent.substring(sVer + 7);
  if ((sVer = gAgent.indexOf("Version")) != -1) fullVer = gAgent.substring(sVer + 8);
} else if ((sVer = gAgent.indexOf("Firefox")) != -1) {
  gName = "Firefox";
  fullVer = gAgent.substring(sVer + 8);
} else if ((sName = gAgent.lastIndexOf(' ') + 1) < (sVer = gAgent.lastIndexOf('/'))) {
  /* 多數瀏覽器中名稱版本是在 userAgent 的結尾*/
  gName = gAgent.substring(sName, sVer);
  fullVer = gAgent.substring(sVer + 1);
  if (gName.toLowerCase() == gName.toUpperCase()) {
    gName = navigator.appName;
  }
}

/*如果分號存在修剪分號*/
if ((sTrim = fullVer.indexOf(";")) != -1) fullVer = fullVer.substring(0, sTrim);
if ((sTrim = fullVer.indexOf(" ")) != -1) fullVer = fullVer.substring(0, sTrim);
majorVer = parseInt('' + fullVer, 10);
if (isNaN(majorVer)) {
  fullVer = '' + parseFloat(navigator.appVersion);
  majorVer = parseInt(navigator.appVersion, 10);
}
document.write("<ul>")
document.write("<li>Browser name = " + gName + "</li>");
document.write("<li>Full version = " + fullVer + "</li>");
document.write("<li>Major version = " + majorVer + "</li>");
document.write("<li>navigator.appName = " + navigator.appName + "</li>");
document.write("</ul>")

navigator window 的特徵


appCodeName 瀏覽器內部編碼名稱

navigator.appCodeName IE, Firefox, Safari, Chrome 均傳回 Mozilla



appName 瀏覽器的正式名稱

navigator.appName IE 傳回 Microsoft Internet Explorer, IE11 傳回 Netscape



Firefox, Safari, Chrome 均傳回 Netscape;Opera 傳回 Opera

appVersion 瀏覽器的版本

navigator.appVersion 傳回格式不太一樣



platform 瀏覽器的平台

navigator.platform 傳回 Win32;IE 64-bit 傳回 Win64



cookieEnabled 瀏覽器的 cookie 是否啟動

navigator.cookieEnabled 傳回瀏覽器的 cookie 是否啟動(布林值) true/false