Because of the version of ie10-ie11, document.all judgment is no longer supported, so the ie judgment function needs to be rewrited.
function isIE() { //ie? if (!!window.ActiveXObject || "ActiveXObject" in window) return true; else return false; }The first type is to distinguish only the browser and not the version
The code copy is as follows:
function myBrowser(){
var userAgent = navigator.userAgent; //Get the browser's userAgent string
var isOpera = userAgent.indexOf("Opera") > -1;
if (isOpera) {
return "Opera"
}; //Judge whether the Opera browser is
if (userAgent.indexOf("Firefox") > -1) {
return "FF";
} //Judge whether the Firefox browser is
if (userAgent.indexOf("Chrome") > -1){
return "Chrome";
}
if (userAgent.indexOf("Safari") > -1) {
return "Safari";
} //Judge whether Safari browser
if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
return "IE";
}; //Judge whether IE browser
}
//The following is the call to the above function
var mb = myBrowser();
if ("IE" == mb) {
alert("I am IE");
}
if ("FF" == mb) {
alert("I am Firefox");
}
if ("Chrome" == mb) {
alert("I am Chrome");
}
if ("Opera" == mb) {
alert("I am Opera");
}
if ("Safari" == mb) {
alert("I am Safari");
}
The second type is to distinguish browsers and consider IE5.5 6 7 8
The code copy is as follows:
function myBrowser(){
var userAgent = navigator.userAgent; //Get the browser's userAgent string
var isOpera = userAgent.indexOf("Opera") > -1; //Judge whether the Opera browser is
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //Judge whether IE browser is
var isFF = userAgent.indexOf("Firefox") > -1; //Judge whether the Firefox browser is
var isSafari = userAgent.indexOf("Safari") > -1; //Judge whether the Safari browser is
if (isIE) {
var IE5 = IE55 = IE6 = IE7 = IE8 = false;
var reIE = new RegExp("MSIE (//d+//.//d+);");
reIE.test(userAgent);
var fIEVersion = parseFloat(RegExp["$1"]);
IE55 = fIEVersion == 5.5;
IE6 = fIEVersion == 6.0;
IE7 = fIEVersion == 7.0;
IE8 = fIEVersion == 8.0;
if (IE55) {
return "IE55";
}
if (IE6) {
return "IE6";
}
if (IE7) {
return "IE7";
}
if (IE8) {
return "IE8";
}
}//isIE end
if (isFF) {
return "FF";
}
if (isOpera) {
return "Opera";
}
}//myBrowser() end
//The following is the call to the above function
if (myBrowser() == "FF") {
alert("I am Firefox");
}
if (myBrowser() == "Opera") {
alert("I am Opera");
}
if (myBrowser() == "Safari") {
alert("I am Safari");
}
if (myBrowser() == "IE55") {
alert("I am IE5.5");
}
if (myBrowser() == "IE6") {
alert("I am IE6");
}
if (myBrowser() == "IE7") {
alert("I am IE7");
}
if (myBrowser() == "IE8") {
alert("I am IE8");
}
Below is a JS code to determine that the current browser is IE.
The principle is made by using the difference between IE and standard browsers in processing arrays. For standard browsers, if the last character in the array is a comma, the JS engine will automatically remove it.
[Ctrl+A Select all Note: If you need to introduce external Js, you need to refresh to execute]