JS检测浏览器版本信息(包含IE11),并动态添加样式
Posted 水墨晨诗
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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),并动态添加样式的主要内容,如果未能解决你的问题,请参考以下文章