首页 > 代码库 > JS检测浏览器版本信息(包含IE11),并动态添加样式

JS检测浏览器版本信息(包含IE11),并动态添加样式

<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="Scripts/jquery-1.9.1.min.js"></script>
    <link href="" rel="stylesheet" /><%--这里可以链接一个默认的样式表(link 标签一定不能丢,不然后面 document.getElementsByTagName("link")[0].href = title;会报错)--%>
    <script>
        $(function () {
            var userAgent = navigator.userAgent,
                rMsie = /(msie\s|trident.*rv:)([\w.]+)/,
                rFirefox = /(firefox)\/([\w.]+)/,
                rOpera = /(opera).+version\/([\w.]+)/,
                rChrome = /(chrome)\/([\w.]+)/,
                rSafari = /version\/([\w.]+).*(safari)/;
            var browser;
            var version;
            var ua = userAgent.toLowerCase();
            function uaMatch(ua) {
                var match = rMsie.exec(ua);
                if (match != null) {
                    return { browser: "IE", version: match[2] || "0" };
                }
                var match = rFirefox.exec(ua);
                if (match != null) {
                    return { browser: match[1] || "", version: match[2] || "0" };
                }
                var match = rOpera.exec(ua);
                if (match != null) {
                    return { browser: match[1] || "", version: match[2] || "0" };
                }
                var match = rChrome.exec(ua);
                if (match != null) {
                    return { browser: match[1] || "", version: match[2] || "0" };
                }
                var match = rSafari.exec(ua);
                if (match != null) {
                    return { browser: match[2] || "", version: match[1] || "0" };
                }
                if (match != null) {
                    return { browser: "", version: "0" };
                }
            }
            var browserMatch = uaMatch(userAgent.toLowerCase());
            if (browserMatch.browser) {
                browser = browserMatch.browser;
                version = browserMatch.version;
            }
            var BVSN = (browser + version);
            if (BVSN == "IE11.0") {
                setStyle("style/ie11.css");//这里是样式表的路径
            }
            else if (BVSN == "IE10.0") {
                setStyle("style/ie10.css");
            }
            else if (BVSN == "IE8.0") {
                setStyle("style/ie8.css");
            }
            else if (BVSN.indexOf("chrome") > -1) {
                setStyle("style/chrome.css");
            }
            else if (BVSN.indexOf("firefox") > -1) {
                setStyle("style/firefox.css");
            }
            var width = window.screen.width;//获取浏览器宽度
            var height = window.screen.height;//获取浏览器高度
        });
        function setStyle(title) {
            document.getElementsByTagName("link")[0].href = title;
        }
    </script>
</head>

 

JS检测浏览器版本信息(包含IE11),并动态添加样式