The comments are all in the code. Very detailed.
I only judged IE Firefox Google because I didn't install any other browsers, so haha. Of course, if you want to judge other browsers. The basic code is the same.
Copy codeThe Code is as follows:
(Function (){
Window. sys = {};
Var ua = navigator. userAgent. toLowerCase ();
// First, let's take a look at what the three browsers return.
// Ie ua = mozilla/4.0 (compatible; msie 8.0; windows nt 5.2; trident/4.0; qqpinyin 730;. net clr 1.1.4322)
// Firefox ua = mozilla/5.0 (windows; u; windows nt 5.2; zh-cn; rv: 1.9.2) gecko/20100115 firefox/3.6
// Chrome ua = mozilla/5.0 (windows; u; windows nt 5.2; en-us) applewebkit/532.5 (khtml, like gecko) chrome/4.0.249.0 safari/532.5
// You can see the preceding Browser IE msie 8.0 firefox/3.6 chrome/4.0.249
// We only need to use regular expressions to find out which browser can match the key points in the string.
// See the following regular firefox Regular Expression/firefox \/([\ d \.] + )/
// Firefox \/matches a character like firefox/
// ([\ D \.] +) () indicates the character to be captured. It is his credit to use [1] to obtain the version number.
// [] Indicates that \ d \. is included as a whole.
// \ D match a number \. Match A. Add together to match 8. Something like this
// [] + [] There is a + outside, which will repeat the originally matched items in [] and turn it into this 8.8.8.8. Of course, this can also be 48.1.563
Sys. firefox = ua. match (/firefox \/([\ d \.] + )/);
Sys. ie = ua. match (/msie \ s ([\ d \.] + )/);
// Msie \ s ([\ d \.] +)
// Ie is basically the same as Firefox.
// Msie 8.0 \ s is a space matching between msie and 8.0
Sys. chrome = ua. match (/chrome \/([\ d \.] + )/);
// Chrome \/([\ d \.] +)
// Same as Firefox
})();
// Use
If (sys. firefox ){
Alert (true)
} Else {
Alert (false)
}
If (sys. ie [1] = "8.0 "){
Alert (true)
} Else {
Alert (false)
}
Alert (sys. firefox );
Alert (sys. ie );
Alert (sys. chrome );
// Only the current browser's sys can use [1] to return the version number
Alert (sys. firefox [1]);
// Alert (sys. ie [1]);
// Alert (sys. chrome [1]);
I encountered a problem. I can see that the experts wrote this.
Firefox \/([\ d.] +) // expert
Firefox \/([\ d \.] +) // my
// I am confused about the differences between the two. I used a lot of STR tests. I didn't see any difference. // is [\ d.] + the same as [\ d \.] +? Please give answers.