获取浏览器名称和版本号

方法一:Javascript:
 function GetBrowserInfo() {
            var agency = navigator.userAgent;
            var temp;
            var match = agency.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
            if (/trident/i.test(match[1])) {
                temp = /\brv[ :]+(\d+)/g.exec(agency) || [];
                return { name: 'IE', version: (temp[1] || '') };
            }
            if (match[1] == "Chrome") {
                temp = agency.match(/\bOPR\/(\d+)/);
                if (temp != null) {
                    return { name: 'Opera', version: temp[1] };
                }
            }
            match = match[2] ? [match[1], match[2]] : [navigator.appName, navigator.appVersion, '-?'];
            if (temp = agency.match(/version\/(\d+)/i) != null) {
                match.slice(1, 1, temp[1]);
            }
            return { name: match[0], version: match[1] };
        }
 var browser = GetBrowserInfo();
 alert("浏览器名称: " + browser.name + "\r" + "浏览器版本: " + browser.version);

方法二:C#

 public static string GetBrowserInfo()
        {
            //HttpBrowserCapabilities hbc = HttpContext.Current.Request.Browser;
            //string browserName = hbc.Browser.ToString();
            //string browserVersion = hbc.Version.ToString();
            //return string.Format("浏览器名称: " + browserName + "\r" + "浏览器版本: " + browserVersion);

            System.Web.HttpBrowserCapabilities browser = HttpContext.Current.Request.Browser;
            return "Browser Capabilities\n"
                + "Type = " + browser.Type + "<br>"
                + "Name = " + browser.Browser + "<br>"
                + "Version = " + browser.Version + "<br>"
                + "Major Version = " + browser.MajorVersion + "<br>"
                + "Minor Version = " + browser.MinorVersion +"<br>"
                + "Platform = " + browser.Platform + "<br>"
                + "Is Beta = " + browser.Beta + "<br>"
                + "Is Crawler = " + browser.Crawler + "<br>"
                + "Is AOL = " + browser.AOL + "<br>"
                + "Is Win16 = " + browser.Win16 + "<br>"
                + "Is Win32 = " + browser.Win32 + "<br>"
                + "Supports Frames = " + browser.Frames + "<br>"
                + "Supports Tables = " + browser.Tables + "<br>"
                + "Supports Cookies = " + browser.Cookies + "<br>"
                + "Supports VBScript = " + browser.VBScript + "<br>"
                + "Supports JavaScript = " +
                    browser.EcmaScriptVersion.ToString() + "<br>"
                + "Supports Java Applets = " + browser.JavaApplets + "<br>"
                + "Supports ActiveX Controls = " + browser.ActiveXControls
                      + "<br>"
                + "Supports JavaScript Version = " +
                    browser["JavaScriptVersion"] + "<br>";
        }


你可能感兴趣的:(获取浏览器名称和版本号)