码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-08-13 01:12:40      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:http   tor   ide   nbsp   meta   return   chrome   信息   tag   

<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),并动态添加样式

标签:http   tor   ide   nbsp   meta   return   chrome   信息   tag   

原文地址:http://www.cnblogs.com/vichin/p/7352163.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!