JavaScript advanced programming Reading Notes (16) javascript detection browser and operating system-detect. js

Source: Internet
Author: User

Browser Detection Method

1. Object Feature Detection: a common method for determining browser capabilities. This method can be used if you are more concerned with the browser's capabilities and do not care about its actual identity. The common native Ajax write method uses this method to create XMLHttpRequest:Copy codeThe Code is as follows: IXHR: function (){
If (window. ActiveXObject ){
XHR = new ActiveXObject ('Microsoft. xmlhttp ');
} Else if (window. XMLHttpRequest ){
XHR = new XMLHttpRequest ();
} Else {
Return null;
}
}

2. user-agent string detection method: You can use the user-agent string in the browser to parse and determine the method in detect. js below.

Detection platform/Operating System

Use the navigator to fold the platform attribute and the user-agent string to determine the method in detect. js below.

Detect browsers and operating systems-detect. jsCopy codeThe Code is as follows: var sUserAgent = navigator. userAgent;
Var fAppVersion = parseFloat (navigator. appVersion );

Function compareVersions (sVersion1, sVersion2 ){
Var aVersion1 = sVersion1.split (".");
Var aVersion2 = sVersion2.split (".");

If (aVersion1.length> aVersion2.length ){
For (var I = 0; I <aVersion1.length-aVersion2.length; I ++ ){
AVersion2.push ("0 ");
}
} Else if (aVersion1.length <aVersion2.length ){
For (var I = 0; I <aVersion2.length-aVersion1.length; I ++ ){
AVersion1.push ("0 ");
}
}

For (var I = 0; I <aVersion1.length; I ++ ){

If (aVersion1 [I] <aVersion2 [I]) {
Return-1;
} Else if (aVersion1 [I]> aVersion2 [I]) {
Return 1;
}
}

Return 0;
}
Var isOpera = sUserAgent. indexOf ("Opera")>-1;
Var isMinOpera4 = isMinOpera5 = isMinOpera6 = isMinOpera7 = isMinOpera7_5 = false;
If (isOpera ){
Var fOperaVersion;
If (navigator. appName = "Opera "){
FOperaVersion = fAppVersion;
} Else {
Var reOperaVersion = new RegExp ("Opera (// d + //. // d + )");
ReOperaVersion. test (sUserAgent );
FOperaVersion = parseFloat (RegExp ["$1"]);
}
IsMinOpera4 = fOperaVersion> = 4;
IsMinOpera5 = fOperaVersion> = 5;
IsMinOpera6 = fOperaVersion> = 6;
IsMinOpera7 = fOperaVersion> = 7;
IsMinOpera7_5 = fOperaVersion> = 7.5;
}
Var isKHTML = sUserAgent. indexOf ("KHTML")>-1
| SUserAgent. indexOf ("Konqueror")>-1
| SUserAgent. indexOf ("AppleWebKit")>-1;

Var isMinSafari1 = isMinSafari1_2 = false;
Var isMinKonq2_2 = isMinKonq3 = isMinKonq3_1 = isMinKonq3_2 = false;
If (isKHTML ){
IsSafari = sUserAgent. indexOf ("AppleWebKit")>-1;
IsKonq = sUserAgent. indexOf ("Konqueror")>-1;
If (isSafari ){
Var reAppleWebKit = new RegExp ("AppleWebKit /// (// d + (? : //. // D *)?) ");
ReAppleWebKit. test (sUserAgent );
Var fAppleWebKitVersion = parseFloat (RegExp ["$1"]);
IsMinSafari1 = fAppleWebKitVersion> = 85;
IsMinSafari1_2 = fAppleWebKitVersion> = 124;
} Else if (isKonq ){
Var reKonq = new RegExp ("Konqueror // (// d + (? : //. // D + (? : //. // D )?)?) ");
ReKonq. test (sUserAgent );
IsMinKonq2_2 = compareVersions (RegExp ["$1"], "2.2")> = 0;
IsMinKonq3 = compareVersions (RegExp ["$1"], "3.0")> = 0;
IsMinKonq3_1 = compareVersions (RegExp ["$1"], "3.1")> = 0;
IsMinKonq3_2 = compareVersions (RegExp ["$1"], "3.2")> = 0;
}

}
Var isIE = sUserAgent. indexOf ("compatible")>-1
& SUserAgent. indexOf ("MSIE")>-1
&&! IsOpera;

Var isMinIE4 = isMinIE5 = isMinIE5_5 = isMinIE6 = false;
If (isIE ){
Var reIE = new RegExp ("MSIE (// d + //. // d + );");
ReIE. test (sUserAgent );
Var fIEVersion = parseFloat (RegExp ["$1"]);
IsMinIE4 = fIEVersion> = 4;
IsMinIE5 = fIEVersion> = 5;
IsMinIE5_5 = fIEVersion> = 5.5;
IsMinIE6 = fIEVersion> = 6.0;
}
Var isMoz = sUserAgent. indexOf ("Gecko")>-1
&&! IsKHTML;
Var ismin1_1 = smin1_1_4 = ismin1_1_5 = false;
If (isMoz ){
Var reMoz = new RegExp ("rv :( // d + //. // d + (? : //. // D + )?) ");
ReMoz. test (sUserAgent );
Ismin1_1 = compareVersions (RegExp ["$1"], "1.0")> = 0;
Ismin1_1_4 = compareVersions (RegExp ["$1"], "1.4")> = 0;
Ismin1_1_5 = compareVersions (RegExp ["$1"], "1.5")> = 0;
}
Var isNS4 =! IsIE &&! IsOpera &&! IsMoz &&! IsKHTML
& (SUserAgent. indexOf ("Mozilla") = 0)
& (Navigator. appName = "Netscape ")
& (FAppVersion> = 4.0 & fAppVersion <5.0 );
Var isMinNS4 = isMinNS4_5 = isMinNS4_7 = isMinNS4_8 = false;
If (isNS4 ){
IsMinNS4 = true;
IsMinNS4_5 = fAppVersion & gt; = 4.5;
IsMinNS4_7 = fAppVersion & gt; = 4.7;
IsMinNS4_8 = fAppVersion> = 4.8;
}
Var isWin = (navigator. platform = "Win32") | (navigator. platform = "Windows ");
Var isMac = (navigator. platform = "Mac68K") | (navigator. platform = "MacPPC ")
| (Navigator. platform = "Macintosh ");
Var isUnix = (navigator. platform = "X11 ")&&! IsWin &&! IsMac;
Var isWin95 = isWin98 = isWinNT4 = isWin2K = isWinME = isWinXP = false;
Var isMac68K = isMacPPC = false;
Var isSunOS = isMinSunOS4 = isMinSunOS5 = isMinSunOS5_5 = false;
If (isWin ){
IsWin95 = sUserAgent. indexOf ("Win95")>-1
| SUserAgent. indexOf ("Windows 95")>-1;
IsWin98 = sUserAgent. indexOf ("Win98")>-1
| SUserAgent. indexOf ("Windows 98")>-1;
IsWinME = sUserAgent. indexOf ("Win 9x4.90")>-1
| SUserAgent. indexOf ("Windows ME")>-1;
IsWin2K = sUserAgent. indexOf ("Windows NT 5.0")>-1
| SUserAgent. indexOf ("Windows 2000")>-1;
IsWinXP = sUserAgent. indexOf ("Windows NT 5.1")>-1
| SUserAgent. indexOf ("Windows XP")>-1;
IsWinNT4 = sUserAgent. indexOf ("WinNT")>-1
| SUserAgent. indexOf ("Windows NT")>-1
| SUserAgent. indexOf ("WinNT4.0")>-1
| SUserAgent. indexOf ("Windows NT 4.0")>-1
&&(! IsWinME &&! IsWin2K &&! IsWinXP );
}
If (isMac ){
IsMac68K = sUserAgent. indexOf ("Mac_68000")>-1
| SUserAgent. indexOf ("68 K")>-1;
IsMacPPC = sUserAgent. indexOf ("Mac_PowerPC")>-1
| SUserAgent. indexOf ("PPC")>-1;
}
If (isUnix ){
IsSunOS = sUserAgent. indexOf ("SunOS")>-1;
If (isSunOS ){
Var reSunOS = new RegExp ("SunOS (// d + //. // d + (? : //. // D + )?) ");
ReSunOS. test (sUserAgent );
IsMinSunOS4 = compareVersions (RegExp ["$1"], "4.0")> = 0;
IsMinSunOS5 = compareVersions (RegExp ["$1"], "5.0")> = 0;
IsMinSunOS5_5 = compareVersions (RegExp ["$1"], "5.5")> = 0;
}
}

Author: Artwl

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.