Javascript code for detecting browser types and versions _ javascript skills

Source: Internet
Author: User
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! Check the code of the browser and its version

The Code is as follows:


GetBrowser: function (){
Var browser = {
Msie: false, firefox: false, opera: false, safari: false,
Chrome: false, netscape: false, appname: 'unknown ', version: 0
},
UserAgent = window. navigator. userAgent. toLowerCase ();
If (/(msie | firefox | opera | chrome | netscape) \ D + (\ d [\ d.] *)/. test (userAgent )){
Browser [RegExp. $1] = true;
Browser. appname = RegExp. $1;
Browser. version = RegExp. $2;
} Else if (/version \ D + (\ d [\ d.] *). * safari/. test (userAgent) {// safari
Browser. safari = true;
Browser. appname = 'safari ';
Browser. version = RegExp. $2;
}
Return browser. appname + browser. version;
}


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() {
 VarUa = navigator. userAgent;// Obtain client information
 VarB = ua. indexOf ("MSIE");// Check the location of the special string "MSIE"
 if (b < 0) {
  return 0;
 }
 ReturnParseFloat (ua. substring (B + 5, ua. indexOf (";", B )));// Truncate the version number string and convert it to a numeric value
}
 
Alert (getIEVer ());// Return 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.

VarUa = navigator. userAgent. toLowerCase ();// Obtain client information
var info = {
Ie:/msie/. test (ua )&&! /Opera/. test (ua ),// Match with IE
 Op:/opera/. test (ua ),// Match operabrowser
 Sa:/version. * safari/. test (ua ),// Match the Safari browser
 Ch:/chrome/. test (ua ),// Match with Chrome
 Ff:/gecko/. test (ua )&&! /Webkit/. test (ua)// Match the Firefox browser
};
(Info. ie) & alert ("IE browser");
(Info. op) & alert ("Operabrowser");
(Info. sa) & alert ("Safari");
(Info. ff) & alert ("Firefox");
(Info. ch) & alert ("Chrome");

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() {
 VarUa = navigator. userAgent;// Obtain client information
 VarB = ua. indexOf ("MSIE");// Check the location of the special string "MSIE"
 if (b < 0) {
  return 0;
 }
 ReturnParseFloat (ua. substring (B + 5, ua. indexOf (";", B )));// Truncate the version number string and convert it to a numeric value
}
 
Alert (getIEVer ());// Return value 7
Detection Operating System:
VarIsWin = (navigator. userAgent. indexOf ("Win")! =-1 );// If it is a Windows system, true is returned.
VarIsMac = (navigator. userAgent. indexOf ("Mac")! =-1 );// If it is a Macintosh system, true is returned.
VarIsUnix = (navigator. userAgent. indexOf ("X11")! =-1 );// Returns true if it is a Unix system.
VarIsLinux = (navigator. userAgent. indexOf ("Linux")! =-1 );// If it is a Linux system, true is returned.
Most of the content of this article comes from the Javascript journey.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.