3.14.2010

[JavaScript] 如何抓取瀏灠器的版本 (How to auto select browser version by JavaScript) Part II??

基於第一部份所寫的,用一段簡單的JavaScript Source Code寫一個判斷瀏灠器核心的方法,這一次自己寫一個更複雜一點的,可以分別至少12種的瀏灠器 (因為GOSURF, PCMan and MSIE的.userAgent、.appName和.appVersion都一模一樣,不曉得要怎麼分辨....@@):
function getMSIEversion(FLAG) {

    var strMSIEversion;

    if (FLAG == 0)
        strMSIEversion = "Microsoft Internet Explorer ";
    else
        strMSIEversion = "MSIE ";

    with(navigator) {
        if (appVersion.charAt(0) == 2)
            strMSIEversion += "2.0";
        else if (appVersion.charAt(0) == 3)
            strMSIEversion += "3.0";
        else { // if(appVersion.charAt(0) == 4)
            if (appVersion.indexOf("MSIE 8") != -1)
                strMSIEversion += "8.0";
            else if (appVersion.indexOf("MSIE 7") != -1)
                strMSIEversion += "7.0";
            else if (appVersion.indexOf("MSIE 6") != -1)
                strMSIEversion += "6.0";
            else if (appVersion.indexOf("MSIE 5") != -1)
                strMSIEversion += "5.0";
            else // if(appVersion.indexOf("MSIE 4") != -1)
                strMSIEversion += "4.0";
        }
    } // with() end!!

    return strMSIEversion;
}
function Init() {

    var strLanguage = navigator.userLanguage;
    var strPlatForm = navigator.platform;
    var strUserAgent = navigator.userAgent;
    var strAppName = navigator.appName;
    var strAppVersion = navigator.appVersion;
    var strBrowserType, strBrowserVersion, strBrowserKernelVersion;
    var nRet = 0;

    with(navigator) {
        if (appName == "Microsoft Internet Explorer") { // Avant Browser, GOSURF, GreenBrowser, KKMan, Maxthon, PCMan, Sleipnir, The World Browser and MSIE.
            if ((userAgent.indexOf("Avant Browser") != -1) && (appVersion.indexOf("Avant Browser") != -1)) {
                strBrowserType = "Avant Browser";
                strBrowserVersion = getMSIEversion(1);
            }
            else if ((userAgent.indexOf("GreenBrowser") != -1) && (appVersion.indexOf("GreenBrowser") != -1)) {
                strBrowserType = "GreenBrowser";
                strBrowserVersion = getMSIEversion(1);
            }
            else if ((userAgent.indexOf("KKMAN") != -1) && (appVersion.indexOf("KKMAN") != -1)) {
                strBrowserType = "KKMAN";
                nRet = appVersion.indexOf("KKMAN") + "KKMAN".length;
                strBrowserVersion = appVersion.slice(nRet, nRet + 3);
            }
            else if ((userAgent.indexOf("Maxthon") != -1) && (appVersion.indexOf("Maxthon") != -1)) {
                strBrowserType = "Maxthon";
                nRet = appVersion.indexOf("Maxthon") + "Maxthon".length + 1;
                strBrowserVersion = appVersion.slice(nRet, nRet + 3);
            }
            else if ((userAgent.indexOf("Sleipnir") != -1) && (appVersion.indexOf("Sleipnir") != -1)) {
                strBrowserType = "Sleipnir";
                nRet = appVersion.indexOf("Sleipnir") + "Sleipnir".length + 1;
                strBrowserVersion = appVersion.slice(nRet, nRet + 5);
            }
            else if ((userAgent.indexOf("TheWorld") != -1) && (appVersion.indexOf("TheWorld") != -1)) {
                strBrowserType = "The World Browser";
                strBrowserVersion = getMSIEversion(1);
            }
            else {// GOSURF, PCMan and Microsoft Internet Explorer
                strBrowserType = "Microsoft Internet Explorer / PCMan / GOSURF";
                strBrowserVersion = getMSIEversion(0);
            }
        }
        else if (appName == "Netscape") { // Mozilla Firefox, Netscape Navigator, Safari, Google Chrome.
            if (userAgent.indexOf("Firefox") != -1) {
                if (userAgent.indexOf("Navigator") != -1) {
                    strBrowserType = "Netscape Navigator";
                    nRet = userAgent.indexOf("Navigator") + "Navigator".length + 1;
                    strBrowserVersion = userAgent.slice(nRet, nRet + 7);
                }
                else {
                    strBrowserType = "Mozilla Firefox";
                    nRet = userAgent.indexOf("Firefox") + "Firefox".length + 1;
                    strBrowserVersion = userAgent.slice(nRet, nRet + 3);
                }
            }
            else if (userAgent.indexOf("Safari") != -1) {
                if (userAgent.indexOf("Chrome") != -1) {
                    strBrowserType = "Google Chrome";
                    nRet = appVersion.indexOf("Chrome") + "Chrome".length + 1;
                    strBrowserVersion = appVersion.slice(nRet, nRet + 10);
                }
                else {
                    strBrowserType = "Safari";
                    nRet = appVersion.indexOf("Safari") + "Safari".length + 1;
                    strBrowserVersion = appVersion.slice(nRet, nRet + 9);
                }
            }
            else {// Other Netscape browser
            }
            strBrowserKernelVersion = appName + " " + appVersion.slice(0, 3);
        }
        else {//if(appName == "Opera")
            if (userAgent.indexOf("Opera") != -1) {
                strBrowserType = "Opera";
                strBrowserVersion = appVersion.slice(nRet, nRet + 4);
            }
        }
        strBrowserKernelVersion = appName + " " + appVersion.slice(0, 4);
    } // with() end!!
    alert("Your Browser Languagr: " + strLanguage + "\n\n" +
        "Your Platform: " + strPlatForm + "\n\n" +
        "Your User Agent:\n" + strUserAgent + "\n\n" +
        "Your Application Name: " + strAppName + "\n\n" +
        "Your Application Version:\n" + strAppVersion + "\n\n" +
        "Your Browser Version: " + strBrowserType + " " + strBrowserVersion + "\n\n" +
        "Your Browser Kernel Version: " + strBrowserKernelVersion + "\n");

    return;
}
補充一下,關於抓取瀏灠器的語系方面,自己原本是使用.language,不過這個物件,在以MSIE為核心的一系列瀏灠器會有問題,會抓不到語系,反而會告訴你"undefined"。

所以,比較佳的解決方法是改用.userLanguage,如此不論是MSIE、Netscape或Opera都能夠正確地抓到語系了。

那麼,所得到的語系,例如:zh-TW,TW是指Taiwan,這大家應該都很好理解;那zh呢??它是指"Zhong Hua (中華)"的音譯。

參考網址:
http://blog.xuite.net/emisjerry/tech/337730

沒有留言:

張貼留言