How to determine the browser version based on JS

This article mainly introduces how to judge the browser version based on JS, in the text, through the sample code, have a certain reference value for everyone’s learning or work, the friends needed can be referred to

Browser Type Judgment

Does not consider the judgment of the browser below IE9

  function getBrowSertype () {var userAgent = navigator.userAgent; // get the user's useERAGENT string var browser = 'unknown'; if (UseERAGENT.INDEXOF ("IE")! = - 1) {Browser = "IE";} else if (UseRegent.indexof ('firefox') ! = - 1) {Browser = "firefox";} else IF ('opr')! = - 1) {Browser = "Opera";} else if (UseERAGENT.INDEXOF ('chrome')! = -1) {Browser = "chrome";} else if ('Safari')! = - 1) {Browser = "Safari";} else if ('Trident')! = - 1 ) {Browser = 'IE 11';}  
Test

judges the IE browser Version 如何基于js判断浏览器版本

includes EDGE, IE11, 10, 9, 8, 7, 6, 5, 4

// Get the IE version function ieversion () {// get the useerAgent string var useeragent = navigator.USERAGENT; / / Decision is smaller than IE11 browser var islessie11 = useERAGENT .INDEXOF (‘Compatible’)> -1 && UseRagent.indexof (‘msie’)> -1; // Judgment whether it is an IE EDGE browser var islessge = useERAGENT.INDEXOF (‘Edge’)> -1 &&! iSlessie11 ; // Judgment whether it is IE11 browser var isie11 = useagent.indexof (‘trident’)> -1 && useERAGENT.INDEXOF (‘RV: 11.0’)> -1; if (islessie11) {var {var = new regexp (‘ MSIE (\\ D + \\. \\ D +); ‘); // Regular expression matches the digital part after Msie in the userAgent string of the browser, this step is not omitted! ! ! Ineg.test (useERAGENT); // Take the value in the first parentheses in the first parentheses VAR IeversionNum = PARSEFLOAT (regexp [‘$ 1’]); if (ieversionnum === 7) {// ie7 return 7} else if (ieversionnum === 8) {// ie8 return 8} else if (ieversionn === 9){// ie9 return 9} else if (ieversionnum === 10) {// ie10 return 10} else {// IE version
  judges the browser model and version Number <7
      return 6
    }
  } else if (isEdge) {
    // edge
    return 'edge'
  } else if (isIE11) {
    // IE11
    return 11
  } else {
    // 不是ie浏览器
    return -1
  }
} 
function getBroswrandversion () {var OS = navigator.platform; var needent = navigator.USERAGENT; var info = “; var temparray =” “; // judgment Browser version var isopera = useERAGENT.INDEXOF (“Opera”)> -1; // Judgment whether Opera Browser Var isie = UseRagent.indexof (“Compatible”> -1 && UseERAGENT.INDEXOF (“Msie”> – 1 &&! Isopera; // Decision IE IE ISEDGE = USERAGENT.TOLOWERCASE (). IndexOf (“Edge”)> -1 &&! Isie; // Judgment whether IE EDGE Browser VAR ISIE11 = (UseERAGENT.TOLOWERCASE () .indexof (“Trident”)> -1 && useERAGENT.INDEXOF (“RV”)> -1); if (/[ff]irefox(\/\d +\.\d + )/.test (USERAGENT)) {Temparray = / ([ff] Irefox) \ / (\ d + \. \D +) /. EXEC (UseERAGENT); Info + = Temparray [1] + Temparray [2];} else if (isie) {var version = “”; var reie = new regexp (“msie (\\ d + \\. \\ d +); “); REIE.TEST (USERAGENT); var fieversion = parsefloat (regexp [” $ 1 “]); if (fieversion == 7) {Version =” ie7 “;} else} (fieversion == 8 ) {version = “IE8”;} else if (fieversion == 9) {version = “ie9”;} else if (fieversion == 10) {version = “ie10”;} else {version = “0”} info + = Version;} else if (iSedge) {INFO + = “edge”;} else if (isie11) {info + = “IE11″;} else if (/[cc]hromem \ \d +/.test ” )) {tempArray = /([cc]hrome) // /;; INFO + = Temparray [1] + Temparray [2];} else if (/ [vv] excer \ / \ D + \. \ d + \. \ d + (\. \ D) * * [SS] AFARI / .TEST (useERAGENT)) {TEMPARRAY = / /[vv]rsion \ / / \ [\ \ \d) * ([SS] AFARI) /.exec (USERAGENT); INFO + = Temparray [3] + Temparray [1];} else if (/ [/ [/ [/ [μPera.+[vv]rsion\/\d +\. \ /.test (USERAGENT)) {TEMPARRAY = /([oo]pera ).+[vv]ers \/(\ \ \ \eren “; info + = Temparray [1] + Temparray [2];} else {info + = “unknown”;} return info;}

below IE9 version reminder
   
[ VAR BRO = getBrosWerandVersion (); if (Bro == “IE5” || Bro == “IE6” || Bro == “IE7” || Bro == “IE8”) {Alert (“browser version Low! Please upgrade to IE9 or more “);}

The above is all the content of this article, I hope to help everyone, I hope Everyone supports Tumi Cloud.
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment