Detection of browser and operating system JavaScript Advanced Programming

/ *
* detect browsers and operating systems
* Book: JavaScript Advanced Programming
* Author: Nicholas C. Zakas
* in the client browser detection is the most important object is the navigator object, which contains some of the browser can provide you with information property
* /
/ *
way * Detect Browser:
* an object / feature detection method: A common method to determine browser capabilities, involving the use of a given object before the first detection of its presence
* For example:
* if (document.getElementById) [If a property or method does not exist, it will return undefined, has been translated into Boolean is false
* {
* / / the method exists, so use it here
*} else {
* / / do something else
*}
* If you are more concerned about the browser and the ability to care about its real identity, you can use this detection method
*
* 2.user-agent string detection method
* user-agent string detection method is the oldest browser detection method Visit the website of each program must provide a user-agent string to determine its identity to the server.
* The introduction of the navigator object in JavaScript in the userAgent property to provide client access to user-agent string
* var sUserAgent = navigator.userAgent;
* on user-agent string between history and various browsers user- Differences agent string format details, see the contents of Chapter VIII of the
* /

/ / save user-agent string
var sUserAgent = window . Navigator . userAgent ; / / save the browser version var fAppVersion = parseFloat ( window . Navigator . appVersion ); / * * accept two versions of string as a parameter, returns 0 if they are equal, if the first is greater than the second returns 1, if the second is greater than the first return -1 * Parameters sVersion example: 0.9.2; 0.9; 1.13 .2; 1.14 * / Function compareVersions ( sVersion1 , sVersion2 ) { var aVersion1 = sVersion1 . Split ( "." ); var aVersion2 = sVersion2 . Split ( "." ); if ( aVersion1 . length < aVersion2 . length ) { for ( var i = 0 ; i < aVersion2 . length - aVersion1 . length ; i + + ) aVersion1 . PUSH ( "0" ); } else if ( aVersion2 . length < aVersion1 . length ) { for ( var i = 0 ; i < aVersion1 . length - aVersion2 . length ; i + + ) aVersion2 . PUSH ( "0" ); } for ( var i = 0 ; i < aVersion1 . length ; i + + ) { var iVal1 = parseInt ( aVersion1 [ i ] , 10 ); var ival2 = parseInt ( aVersion2 [ i ] , 10 ); if ( iVal1 < ival2 ) return - 1 ; else if ( iVal1 > ival2 ) return 1 ; } return 0 ; } / * Opera * detection * most convenient The judge is to search the user-agent string, there is no Opera * then continue to determine the version actually used * / var isOpera = sUserAgent . indexOf ( "Opera" ) > - 1 ; var isMinOpera4 = isMinOpera5 = isMinOpera6 = isMinOpera7 = isMinOpera7_5 = false ; if ( isOpera ) { var fOperaVersion ; if ( window . 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 ; } / * * Detection Konqueror / Safari * Konqueror / Safari is based on KHTML project * / var isKHTML = sUserAgent . indexOf ( "KHTML" ) > - 1               | | sUserAgent . indexOf ( "Konqueror " ) > - 1               | | sUserAgent . indexOf ( "AppleWebKit" ) > - 1 ; if ( isKHTML ) { isSafari = sUserAgent . indexOf ( "AppleWebKit" ) > - 1 ; isKonq = sUserAgent . indexOf ( "Konqueror" ) > - 1 ; var isMinSafari1 = isMinSafari1_2 = false ; var isMinKonq2_2 = isMinKonq3 = isMinKonq3_1 = isMinKonq3_2 = false ; 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" ); } } / * * detect IE * / 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 ; } / * * Detection Mozilla * / var isMoz = sUserAgent . indexOf ( "Gecko" ) > - 1 && ! ​​isKHTML ; var isMinMoz1 = isMinMoz1_4 = isMinMoz1_5 = false ; if ( isMoz ) { var reMoz = new RegExp ( "RV: (\ \ d + \ \. \ \ d +? (:?. \ \ \ \ d +)) " ); reMoz . test ( sUserAgent ); isMinMoz1 = compareVersions ( RegExp [ "$ 1" ] , "1.0" ) > = 0 ; isMinMoz1_4 = compareVersions ( RegExp [ "$ 1" ] , "1.4" ) > = 0 ; isMinMoz1_5 = compareVersions ( RegExp [ "$ 1" ] , "1.5" ) > = 0 ; } / * Netscape Communicator 4.x * detection * Although Netscape Communicator outdated, but It's possible there are still some users * / 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 ) { isNS4 = true ; isMinNS4_5 = fAppVersion > = 4.5 ; isMinNS4_7 = fAppVersion > = 4.7 ; isMinNS4_8 = fAppVersion > = 4.8 ; } / * * platform / operating system detection script * determine the operating system should start to find a platform to start, the platform is mainly divided into three categories: Windows, Macintosh, and Unix * / var isWin = ( window . Navigator . Platform == "Win32" ) | | ( window . Navigator . Platform == "Windows" ); var isMac = ( window . Navigator . Platform == "Mac68k" ) | | ( window . Navigator . Platform == "MacPPC" ); var isUnix = ( window . Navigator . Platform == "X11" ) && ! ​​isWin && ! ​​isMac ; var isWin95 = isWin98 = isWinNT4 = isWin2K = isWinME = isWinXP = 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 9x 4.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 ); } var isMac68K = isMacPPC = false ; if ( isMac ) { isMac68K = sUserAgent . indexOf ( "Mac_68000" ) > - 1 | | sUserAgent . indexOf ( "68K" ) > - 1 ; isMacPPC = sUserAgent . indexOf ( " Mac_PowerPC " ) > - 1 | | sUserAgent . indexOf ( "PPC" ) > - 1 ; } / * Unix * There is considerable style, SunOS, HP-UX, AIX, Linux, etc., there are different versions of each as well as different user-agent string representation * here only concern and some versions of SunOS, you should have enough knowledge to be transformed into a script to detect other Unix platforms * / var isSunOS = isMinSunOS4 = isMinSunOS5 = isMinSunOS5_5 = false ; 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 ; } }


Learn More :