JQuery is used to determine the browser type. The usage of the tool class $. browser is mainly used:
$. Browser. ['browser keyword ']
The Code is as follows:
$ (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! ");
}
Let's take a look at the jQuery source code:
The Code is as follows:
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)
};
JQuery uses regular expressions to match userAgent to determine the browser type and version.
How can we determine whether the current browser is IE6?
$. Browser. msie & ($. browser. version = "6.0 ")&&! $. Support. style
Similarly, jQuery checks whether the browser is IE7.
$. Browser. msie & ($. browser. version = "7.0 ")
If backward compatibility is not considered, you do not want to import jQuery to determine the browser type.
The simplest way to judge IE is
The Code is as follows:
If (document. all ){
Alert ("IE6 ")
}
$. Browser uses regular expressions to match userAgent to determine the browser version and type. jquery has been declared in the jquery1.3.2 document. browser and jquery. browser. we recommend that you discard the version. You can use jquery. to replace
But in the current situation, jquery. support is not easy to use and is very difficult to use. We still honestly use $. browser to judge the browser type.
To determine the version of IE, we recommend that you use the conditional expression of IE to write JavaScript code.
The Code is as follows:
This is more accurate than we manually use $. browser to determine the IE version, and we do not need to record the use of jquery's browser.