用于 Internet Explorer 浏览器版本用户代理的 Javascript 重定向?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7407182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript redirect for Internet Explorer browser version user agent?
提问by ian_6500
I found out that my javascript-intensive web site doesn't work reliably (or at all) in IE9.
我发现我的 javascript 密集型网站在 IE9 中不能可靠地(或根本不能)运行。
It works, (usually, but not always) with the compatibility mode meta tag in the header, but I just want to build a page that I know will work well in IE9 and then have the usual page redirect to it when IE9 is detected. The usual page is fine in IE 7 and 8 (and every other browser I've tried it on).
它可以(通常,但不总是)与标题中的兼容模式元标记一起工作,但我只想构建一个我知道可以在 IE9 中正常工作的页面,然后在检测到 IE9 时将通常的页面重定向到它。通常的页面在 IE 7 和 8(以及我尝试过的所有其他浏览器)中都很好。
Can anyone give me some javascript that will do that? Thank you!
谁能给我一些可以做到这一点的javascript?谢谢!
Here's my usual page:
这是我通常的页面:
回答by Bengel
The simplest way would be to use IE Conditionals.
最简单的方法是使用IE Conditionals。
Note:IE10 and beyond have removed supportfor this feature. For modern browsers the widely accepted way of conditionally displaying content for compatibility purposes is using feature detection. Modernizris a popular library built for handling feature detection.
注意:IE10 及更高版本已取消对此功能的支持。对于现代浏览器,出于兼容性目的而有条件地显示内容的广泛接受的方式是使用特征检测。Modernizr是一个流行的库,用于处理特征检测。
For example:
例如:
<!--[if IE 9]>
<script type="text/javascript">
window.location = "http://www.ie9version.com";
</script>
<![endif]-->
Examples from the conditional site:
来自条件站点的示例:
<!--[if IE]><p>You are using Internet Explorer.</p><![endif]-->
<![if !IE]><p>You are not using Internet Explorer.</p><![endif]>
<!--[if IE 7]><p>Welcome to Internet Explorer 7!</p><![endif]-->
<!--[if !(IE 7)]><p>You are not using version 7.</p><![endif]-->
<!--[if gte IE 7]><p>You are using IE 7 or greater.</p><![endif]-->
<!--[if (IE 5)]><p>You are using IE 5 (any version).</p><![endif]-->
<!--[if (gte IE 5.5)&(lt IE 7)]><p>You are using IE 5.5 or IE 6.</p><![endif]-->
<!--[if lt IE 5.5]><p>Please upgrade your version of Internet Explorer.</p><![endif]-->
<!--[if true]>You are using an <em>uplevel</em> browser.<![endif]-->
<![if false]>You are using a <em>downlevel</em> browser.<![endif]>
<!--[if true]><![if IE 7]><p>This nested comment is displayed in IE 7.</p><![endif]><![endif]-->
回答by ian_6500
<script LANGUAGE="JavaScript">
<!--
if( navigator.appName.toLowerCase().indexOf("microsoft") > -1 ||
navigator.userAgent.toLowerCase().indexOf("msie") > -1 ) {
window.open("http://www.pobox.com/~qed/windoze.html", "Windoze",
"dependent=no,titlebar=no,scrollbars=yes" );
}
// Paul Hsieh
// qed at pobox dot com
// -->
</script>