gpt4 book ai didi

javascript - 使用 appCodeName 检测浏览器并加载一些 css

转载 作者:行者123 更新时间:2023-11-28 11:16:32 24 4
gpt4 key购买 nike

我想检测浏览器,然后我需要根据浏览器加载 css 文件。为此我使用以下 javascript。但问题是我使用的任何浏览器都显示 Mozilla 作为代号。

<script type="text/javascript">
browsername=navigator.appCodeName;
alert(browsername); //<- always alert Mozilla if i run in IE or Safari
</script>

第二件事是如果浏览器是 IE,那么我需要加载一些 css。

最佳答案

您可以使用条件注释来加载 CSS 文件:

<p class="accent">
<!--[if IE]>
According to the conditional comment this is IE<br />
<![endif]-->
<!--[if IE 6]>
According to the conditional comment this is IE 6<br />
<![endif]-->
<!--[if IE 7]>
According to the conditional comment this is IE 7<br />
<![endif]-->
<!--[if IE 8]>
According to the conditional comment this is IE 8<br />
<![endif]-->
<!--[if IE 9]>
According to the conditional comment this is IE 9<br />
<![endif]-->
<!--[if gte IE 8]>
According to the conditional comment this is IE 8 or higher<br />
<![endif]-->
<!--[if lt IE 9]>
According to the conditional comment this is IE lower than 9<br />
<![endif]-->
<!--[if lte IE 7]>
According to the conditional comment this is IE lower or equal to 7<br />
<![endif]-->
<!--[if gt IE 6]>
According to the conditional comment this is IE greater than 6<br />
<![endif]-->
<!--[if !IE]> -->
According to the conditional comment this is not IE 5-9<br />
<!-- <![endif]-->
</p>

下面是一些使用 JavaScript 检测浏览器的代码:

navigator.sayswho = (function () {
var N = navigator.appName,
ua = navigator.userAgent,
tem;
var M = ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
if (M && (tem = ua.match(/version\/([\.\d]+)/i)) != null) M[2] = tem[1];
M = M ? [M[1], M[2]] : [N, navigator.appVersion, '-?'];

return M;
})();

示例:http://jsfiddle.net/32cj6/

关于javascript - 使用 appCodeName 检测浏览器并加载一些 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21735622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com