0

如果有人使用任何版本的 Internet Explorer 访问该页面,我希望显示一条警报消息。我读过的所有帖子都说要使用,但它似乎对我不起作用。

这是我的代码:

 <head>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
 .
 .
 .
  <!--[if IE]>
  <script>
    $(document).ready(function(){
        alert('This website is not compatible with internet explorer. 
        Please visit the page with any other browser.');
    });
  </script>
  <![endif]-->
 </head>

难道我做错了什么?

4

3 回答 3

3

I have come to a simple, short and fast method to detect the actual browser being used via javascript, regardless of whether the user-agent is being overridden manually or not, which is even more reliable than the (now deprecated) jQuery.browser sniffing method. This one relies on looking at the window.navigator.productSub property.

In your case, for detecting IE only, this should work in all modern browsers:

if(!window.navigator.productSub && window.navigator.appName !== "Opera")

It works because only IE and Opera have navigator.productSub as "undefined". And the Blink engine has the same as Webkit. Konqueror and all branches of Safari have navigator.productSub = 20030107.

While I have yet to fully determine is this navigator.productSub is reliable for ALL browser, the following information suggest it's a safe one for 99% of cases, even for good old Netscape: https://developer.mozilla.org/en-US/docs/DOM/window.navigator.productSub http://www.billpegram.com/JavaScript/navigatorproperties.html

PS: And for some more history on navigator.productSub timestamps: Why does navigator.productSub always equals '20030107' on Chrome and Safari?

于 2013-04-28T01:08:30.927 回答
1

如果您真的想为所有使用 Internet Explorer 版本 5 到 9 的 Internet Explorer 用户运行代码,那么您的代码就很好。这些是唯一支持条件注释的版本。

可以在此处找到替代解决方案,以及我在此站点上最喜欢的答案之一。它已经在 Internet Explorer 版本 6 到 10 中进行了测试,它会查找 Internet Explorer 特定的函数,如果该函数存在,它将执行脚本。

于 2013-04-28T00:02:43.787 回答
0

尝试这样的事情

<!--[if = IE 6]>
<script>
  alert("6");
</script>
<[endif]-->

<!--[if = IE 7]>
<script>
  alert("7");
</script>
<[endif]-->

<!--[if = IE 8]>
<script>
 alert("8");
</script>
<[endif]-->

我没有IE所以无法测试,抱歉

显然这不适用于最新版本的 IE

jQuery 还删除了 jQuery.browser并推荐使用jQuery.support进行特征检测

显然,当您尝试检测它们时,IE 会对某些功能返回 false。

微软曾经维护一个页面(Detecting Windows Internet Explorer More Effectively)解释如何检测ie,但自2011年以来不再维护

您可以使用Modernizr进行功能检测,并根据浏览器支持的功能做出决定。

恐怕这是我在这个问题上所拥有的最好的了。

于 2013-04-27T23:54:05.743 回答