The example of this article describes the JS code to achieve browser detection, sharing for everyone to reference, the specific content as follows
var browsermatch = {init:function () {this.browser = This.getbrowser (). browser | |
"An Unknown Browser"; This.version = This.getbrowser (). version | |
"An Unknown Version"; This. OS = This.getos () | |
"An Unknown OS";
}, Getos:function () {if (Navigator.platform.indexOf ("Win")!=-1) return "Windows";
if (Navigator.platform.indexOf ("Mac")!=-1) return "Mac";
if (Navigator.platform.indexOf ("Linux")!=-1) return "Linux";
if (Navigator.userAgent.indexOf ("IPhone")!=-1) return "Iphone/ipod"; }, Getbrowser:function () {var Rmsie =/(MSIE\S|TRIDENT\/7) ([\w\.]
+)/; var rtrident =/(trident) \/([\w.]
+)/; var rfirefox =/(Firefox) \/([\w.]
+)/; var Ropera =/(opera). +version\/([\w.]
+)/;
var Rnewopera =/(OPR) \/(. +)/; var rchrome =/(Chrome) \/([\w.]
+)/; var Rsafari =/version\/([\w.]
+). * (Safari)/;
var ua = Navigator.userAgent.toLowerCase ();
var matchbs,matchbs2;
Matchbs = Rmsie.exec (UA); if (Matchbs!= null) {Matchbs2 = rtrident.exec (UA);
if (matchBS2!= null) {switch (matchbs2[2]) {case ' 4.0 ': return {browser: "IE", Version: "8"};break;
Case "5.0": return {browser: "IE", Version: "9"};break;
Case "6.0": return {browser: "IE", Version: "Ten"};break;
Case "7.0": Return {browser: "IE", Version: "One"};break;
Default:return {browser: "IE", Version: "Undefined"}; }}else{return {browser: "IE", version:matchbs[2] | |
"0"};
} Matchbs = Rfirefox.exec (UA); if ((Matchbs!= null) && (!) ( window.attachevent) && (!) ( Window.chrome) && (!) ( Window.opera)) {return {browser:matchbs[1] | | "", Version:matchbs[2] | | "
0 "};
} Matchbs = Ropera.exec (UA); if ((Matchbs!= null) && (!) ( window.attachevent)) {return {browser:matchbs[1] | | "", Version:matchbs[2] | | "
0 "};
} Matchbs = Rchrome.exec (UA); if ((MATCHBS!= null) && (!! (Window.chrome)) && (!) (
window.attachevent))) {matchBS2 = Rnewopera.exec (UA); if (matchBS2 = null) {return {browser:matchbs[1] | | "", Version:matchbs[2] | | "
0 "}; }else{return {browser: "Opera", version:matchbs2[2] | |
"0"};
} Matchbs = Rsafari.exec (UA); if ((Matchbs!= null) && (!) ( window.attachevent) && (!) ( Window.chrome) && (!) ( Window.opera)) {return {browser:matchbs[2] | | "", Version:matchbs[1] | | "
0 "};
}
}
};
Browsermatch.init ();
Call method: (first have to introduce JS file, we should all understand)
Get Browser name: Browsermatch.browser;
Get Browser version: browsermatch.version;
get the operating system: Browsermatch.os;
The above is the entire content of this article, I hope to help you learn.