The browser I used previously was a self-written js method. The principle was that I used js to get the userAgent value and then made the indexOf judgment. Now I used jquery $. browser is not only simple but also highly compatible. I will introduce it to you below.
Let's take a look at the jQuery source code:
| The Code is as follows: |
Copy code |
Var userAgent = navigator. userAgent. toLowerCase (); // Figure out what browser is being used JQuery. browser = { Version: (userAgent. match (/. + (? : Rv | it | ra | ie) [/:] ([d.] +)/) | []) [1], Safari:/webkit/. test (userAgent ), Opera:/opera/. test (userAgent ), Msie:/msie/. test (userAgent )&&! /Opera/. test (userAgent ), Mozilla:/mozilla/. test (userAgent )&&! /(Compatible | webkit)/. test (userAgent) }; |
Test usage
$. Browser. ['browser keyword ']
| The Code is as follows: |
Copy code |
$ (Function (){ If ($. browser. msie ){ Alert ("this is msie "); } Else if ($. browser. safari) { Alert ("this is safari! "); } Else if ($. browser. mozilla) { Alert ("this is mozilla! "); } Else if ($. browser. opera ){ Alert ("this is opera "); } Else { Alert ("I don't konw! "); } |
If the browser above is not enough to judge, we can write it by ourselves. The method is also very simple. Just add your browser name in the jquery function.
If you want to judge IE6, we can perform the following operations:
| The Code is as follows: |
Copy code |
$. Browser. msie & ($. browser. version = "6.0 ")&&! $. Support. style |
If ie6, 7, 8, 9, and 10 are all available, I will not introduce them here.