This article describes whether to check the IE instance code. It can be judged by js or jquery. Next I will introduce it separately.
Js determines whether it is IE
The Code is as follows: |
Copy code |
Function isMSIE (){ Var match =/(msie) ([w.] +)/. exec (navigator. userAgent. toLowerCase ()); If (match! = Null ){ Return true; } Else { Return false; } } |
As shown in jQuery. browser, jQuery discards this method from 1.9 and uses jQuery. support to detect the features supported by the browser.
The Code is as follows: |
Copy code |
<Script type = "text/javascript" src = "jquery-1.3.2.min.js"> </script> <Script> $ (Function (){ If ($. browser. msie ){ Alert ("this is an Internet Explorer "); } Else if ($. browser. opera ){ Alert ("This Is An operabrowser "); } Else if ($. browser. mozilla ){ Alert ("this is a Firefox browser "); } Else if ($. browser. safari ){ Alert ("this is a safari browser "); } }) </Script> |
When the page load type determines the browser type, the types that can be determined include msie, mozilla, opera, and safari.
JQuery claims that the use of navigator. userAgent is inaccurate and is prone to spoofing.
JQuery removed $. browser and $. browser. version from version 1.9 and replaced it with $. support. In the latest version 2.0, IE 6/7/8 is no longer supported. Later, if you need to support IE 6/7/8, you can only use jQuery 1.9. To fully support IE and mix jQuery 1.9 and 2.0, the official solution is:
The Code is as follows: |
Copy code |
<! -- [If lt IE 9]> <Script src = 'jquery-1.9.0.js'> </script> <! [Endif] --> <! -- [If gte IE 9]> <Script src = 'jquery-2.0.0.js '> </script> <! [Endif] --> |
For a long time, this will facilitate processing separately based on browser features in complex cases, rather than simply detecting browser types and versions. However, at present, many old programs cannot be transplanted directly to support the features of the browser, so I have found some solutions that can be replaced directly on the Internet.
Determine the browser type:
The Code is as follows: |
Copy code |
$. Browser. mozilla =/firefox/. test (navigator. userAgent. toLowerCase ()); $. Browser. webkit =/webkit/. test (navigator. userAgent. toLowerCase ()); $. Browser. opera =/opera/. test (navigator. userAgent. toLowerCase ()); $. Browser. msie =/msie/. test (navigator. userAgent. toLowerCase ()); |
The expression after the equal sign returns true/false, which can be directly used to replace the original $. browser. msie.
Check whether IE6 is used:
The Code is as follows: |
Copy code |
// Old If ($. browser. msie & 7> $. browser. version ){} // New If ('undefined' = typeof (document. body. style. maxHeight )){} |
Check whether it is IE 6-8:
The Code is as follows: |
Copy code |
If (! $. Support. leadingWhitespace ){} |
The ultimate method is to replace it with another class library, such as this, but the author does not recommend using browser types and versions for judgment.
However, I still think the detection costs are low and valuable. As for developers to forge it, let them forge it. I just want to do more violent degradation.