Article Introduction: JS judge Ie,ff and other browser types. |
First, browser only, regardless of version
function Mybrowser () {
var useragent = navigator.useragent; Gets the useragent string for the browser
var Isopera = Useragent.indexof ("Opera") >-1;
if (Isopera) {return "Opera"}; Judge whether Opera browser
if (Useragent.indexof ("Firefox") >-1) {return "
FF";} Determine if the Firefox browser
if (Useragent.indexof ("Safari") >-1) {return "Safari";}//Determine if Safari browser
if (Useragent.indexof ("compatible") >-1 && useragent.indexof ("MSIE") >-1 &&!isopera) {return "IE ";} ; Determine if IE browser
}
Here is the function called above
if (mybrowser () = = "ie") {alert ("I am IE");}
if (mybrowser () = "
FF") {alert (" I am Firefox ");}
if (mybrowser () = = "Opera") {alert ("I am Opera");}
if (mybrowser () = = "Safari") {alert ("I am Safari");}
The second is to differentiate the browser and consider IE5.5 6 7 8
function Mybrowser () {
var useragent = navigator.useragent; Gets the useragent string for the browser
var Isopera = Useragent.indexof ("Opera") >-1; Judge whether Opera browser
var Isie = Useragent.indexof ("compatible") >-1 && useragent.indexof ("MSIE") >-1 &&!isopera; Determine if IE browser
var ISFF = Useragent.indexof ("Firefox") >-1; Determine if the Firefox browser
var Issafari = Useragent.indexof ("Safari") >-1; Determine if Safari browser
if (Isie) {
var IE5 = IE55 = IE6 = IE7 = IE8 = false;
var reie = new RegExp ("MSIE (\\d+\\.\\d+);");
Reie.test (useragent);
var fieversion = parsefloat (regexp["$");
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
Here is the function called above
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");}