检测除IE之外的所有浏览器?

时间:2012-10-02 04:50:01

标签: javascript internet-explorer .htaccess redirect

有没有办法检测所有浏览器,但IE用于重定向HTAccess或Javascript?如果不是IE,重定向到site.com?

4 个答案:

答案 0 :(得分:2)

var ie = /msie/i.test(navigator.userAgent);
if (!ie) { location.href = 'http://site.com'; }

答案 1 :(得分:2)

我建议使用IE的条件评论而不是因为这意味着重定向代码甚至不会被IE解释。即IE根本不会运行 JS代码,只有其他浏览器会。

使用JS进行重定向

<!--[if !IE]> -->
    <script>
    window.location.href = 'http://not-ie.com'
    </script>
<!-- <![endif]-->

使用元刷新进行重定向

<!--[if !IE]> -->
    <meta http-equiv="refresh" content="5;url=http://not-ie.com/"> 
<!-- <![endif]-->

重定向前未显示页面的技巧

您可以通过有条件地呈现<body>标记来执行此操作。 免责声明:尚未对此进行测试。

<!-- for non-ie browsers, render the body tag as invisible -->
<!--[if !IE]> -->
    <meta http-equiv="refresh" content="5;url=http://not-ie.com/"> 
    <body style="display: none">
<!-- <![endif]-->

<!-- for ie, render the body tag normally -->
<!--[if IE]> -->
    <body>
<!-- <![endif]-->

<!-- your page content HTML goes here -->

</body></html>

有关条件评论的更多信息http://www.quirksmode.org/css/condcom.html

答案 2 :(得分:0)

你可以这样做:

<!--[if IE 6]><script>location.href='youhazie6.htm';</script><![endif]-->

答案 3 :(得分:0)

您是否理解上述unswer中描述的条件注释方法在IE10中不起作用? IE10不支持条件注释,很快就会发布。

因此,要使代码真正起作用,请依赖于特征检测或依赖于userAgent。 但是当有很多插件的时候,userAgent也会失败,所以“Internet Explorer”或“MSIE”可能会在现实生活中被切断。

为了使检测更可靠,请进行一些功能嗅探:

if ('\v'=='v' || /msie/i.test(navigator.userAgent);) {
  // ah that's you IE!
} 

这就够了。