How does JS judge the browser type and distinguish the browsers of each version of IE in detail
- 2021-07-26 06:19:08
- OfStack
Today, JS is used to judge the browser type, so the system has been sorted out for later use.
/*
* Description: Determine browser information
* Prepare: LittleQiang_w
* Date: 2016.1.5
* Version: V1.1
*/
// Determine the current browsing type
function BrowserType()
{
var userAgent = navigator.userAgent; // Object of the browser userAgent String
var isOpera = userAgent.indexOf("Opera") > -1; // Judge whether Opera Browser
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; // Judge whether IE Browser
var isEdge = userAgent.indexOf("Windows NT 6.1; Trident/7.0;") > -1 && !isIE; // Judge whether IE Adj. Edge Browser
var isFF = userAgent.indexOf("Firefox") > -1; // Judge whether Firefox Browser
var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") == -1; // Judge whether Safari Browser
var isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1; // Judge Chrome Browser
if (isIE)
{
var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
reIE.test(userAgent);
var fIEVersion = parseFloat(RegExp["$1"]);
if(fIEVersion == 7)
{ return "IE7";}
else if(fIEVersion == 8)
{ return "IE8";}
else if(fIEVersion == 9)
{ return "IE9";}
else if(fIEVersion == 10)
{ return "IE10";}
else if(fIEVersion == 11)
{ return "IE11";}
else
{ return "0"}//IE The version is too low
}//isIE end
if (isFF) { return "FF";}
if (isOpera) { return "Opera";}
if (isSafari) { return "Safari";}
if (isChrome) { return "Chrome";}
if (isEdge) { return "Edge";}
}//myBrowser() end
// Determine whether it is IE Browser
function isIE()
{
var userAgent = navigator.userAgent; // Object of the browser userAgent String
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; // Judge whether IE Browser
if(isIE)
{
return "1";
}
else
{
return "-1";
}
}
// Determine whether it is IE Browser, including Edge Browser
function IEVersion()
{
var userAgent = navigator.userAgent; // Object of the browser userAgent String
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; // Judge whether IE Browser
var isEdge = userAgent.indexOf("Windows NT 6.1; Trident/7.0;") > -1 && !isIE; // Judge whether IE Adj. Edge Browser
if(isIE)
{
var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
reIE.test(userAgent);
var fIEVersion = parseFloat(RegExp["$1"]);
if(fIEVersion == 7)
{ return "IE7";}
else if(fIEVersion == 8)
{ return "IE8";}
else if(fIEVersion == 9)
{ return "IE9";}
else if(fIEVersion == 10)
{ return "IE10";}
else if(fIEVersion == 11)
{ return "IE11";}
else
{ return "0"}//IE The version is too low
}
else if(isEdge)
{
return "Edge";
}
else
{
return "-1";// Non IE
}
}
The above code has passed the test and is available!
However, there is a problem, that is, IE5 and IE7 browsers cannot be distinguished for the time being. The following is the information of userAgent under IE5 and IE7; Through userAgent, it is found that IE5 and IE7 cannot be correctly distinguished simply by the above methods. Looking forward to the solution of this problem! ! !
IE5: userAgent "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.2; .NET4.0E)"
IE7: userAgent "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.2; .NET4.0E)"