Document directory
- Object/Feature Detection
- User-Agent string Detection
- Detection Operating System:
If you do not have a deep understanding of JavaScript, it is easy to write incompatible code (like me). At this time, you have to judge the browser. For example, event listening, mouse and keyboard events, and range events are different. The following lists several common browser detection methods to attract viewers!
Object/Feature Detection
This method is a general method for judging the browser capabilities (rather than the exact model of the browser. Most JS experts believe that this method is the most suitable, because they believe that the scripts written according to this method can withstand the test of the future.
// Obtain the IE browser version number
// Return a value indicating the major version number of IE.
function getIEVer() {
VaR UA = navigator. useragent; // obtain user information
VaR B = UA. indexof ("MSIE"); // detects the location of the special string "MSIE"
if (b < 0) {
return 0;
}
Return parsefloat (UA. substring (B + 5, UA. indexof (";", B); // extract the version number string and convert it to a value.
}
Alert (getiever (); // return the value 8 (My IE8)
This method can be used if you are more concerned with the browser's capabilities and do not care about its actual identity.
User-Agent string Detection
The User-Agent string provides a large amount of information about the web browser, including the browser name and version.
VaR UA = navigator. useragent. tolowercase (); // obtain user information
var info = {
IE:/MSIE/. Test (UA )&&! /Opera/. Test (UA), // match IE browser
OP:/Opera/. Test (UA), // match operabrowser
Sa:/version. * Safari/. Test (UA), // match the Safari browser
Ch:/Chrome/. Test (UA), // match the Chrome browser
FF:/Gecko/. Test (UA )&&! /WebKit/. Test (UA) // match the Firefox browser
};
(Info. ie) & alert ("IE browser ");
(Info. OP) & alert ("opera ");
(Info. SA) & alert ("safari ");
(Info. ff) & alert ("Firefox ");
(Info. ch) & alert ("Chrome browser ");
We usually do the most, that is, to determine whether it is IE, and other browsers will generally comply with the standards. Some customers only need to comply with IE and FF to meet the requirements. Then we can do this:
var isIE = (navigator.appName == "Microsoft Internet Explorer");
Judging IE is far more than the above method. You can use more unique features of IE, such as window. activexobject and document. All, which belong to the object/feature detection method! You need to write different styles on different browsers (because the parsing of IE styles varies). You have to determine the version. You can do this.
// Obtain the IE browser version number
// Return a value indicating the major version number of IE.
function getIEVer() {
VaR UA = navigator. useragent; // obtain user information
VaR B = UA. indexof ("MSIE"); // detects the location of the special string "MSIE"
if (b < 0) {
return 0;
}
Return parsefloat (UA. substring (B + 5, UA. indexof (";", B); // extract the version number string and convert it to a value.
}
Alert (getiever (); // return value 7
Detection Operating System:
VaR iswin = (navigator. useragent. indexof ("win ")! =-1); // if it is a Windows system, true is returned.
VaR ismac = (navigator. useragent. indexof ("Mac ")! =-1); // returns true if it is a Macintosh System
VaR isunix = (navigator. useragent. indexof ("X11 ")! =-1); // returns true if it is a UNIX System
VaR islinux = (navigator. useragent. indexof ("Linux ")! =-1); // if it is a Linux system, true is returned.