Js determines the current browser type IE browser method

  • 2020-03-30 03:08:19
  • OfStack

Shortest IE judge method, original address: (link: #)

Var isIE =! - [1]

How it works: [1,] returns "1" in a standard browser, which is equivalent to calling [1,]. ToString (), which returns "1," in IE.
When the return value is cast to a number with a minus sign, the standard browser returns 1, IE returns NaN,
When I invert 1 and NaN, the standard browser returns false and IE returns true.

Js determines the browser type

There is basically nothing wrong with the original text, but his way of judging is not very rigorous.

Such as: the navigator userAgent. IndexOf (" Safari ") > 0. If the word "Safari" appears at the very front of the userAgent, then you won't get the right result. Should put > 0 instead! = 1
Then I tested the existing five independent kernel browsers on my machine and found that Opera's userAgent value was "Opera/9.80 (Windows NT 5.1; U; Ed..."
That's exactly what I said.
In fact, most of the time when we do browser compatibility is mainly for Internet explorer and non-internet explorer browsers, that is, generally only need to determine whether the Internet explorer browser can be.
Determines the current browser type code


<script type="text/javascript" >
    <!-- 
function getOs() 
{ 
    var OsObject = ""; 
   if(isIE = navigator.userAgent.indexOf("MSIE")!=-1) { 
        return "MSIE"; 
   } 
   if(isFirefox=navigator.userAgent.indexOf("Firefox")!=-1){ 
        return "Firefox"; 
   } 
   if(isChrome=navigator.userAgent.indexOf("Chrome")!=-1){ 
        return "Chrome"; 
   } 
   if(isSafari=navigator.userAgent.indexOf("Safari")!=-1) { 
        return "Safari"; 
   }  
   if(isOpera=navigator.userAgent.indexOf("Opera")!=-1){ 
        return "Opera"; 
   } 

} 
 alert("type -> "+getOs());
--> 
</script>


Related articles: