【发布时间】:2014-02-16 07:01:21
【问题描述】:
在我们的索引页面上,我们有脚本可以将智能手机(iPhone、Android 和 Windows Phone)上的用户重定向到我们的移动网站。我们使用的技术是:
if ( navigator.userAgent.match( /iPhone/ ) && ! navigator.userAgent.match( /iPad/ ) ) {
window.location="mobile.html";
}
else if ( navigator.userAgent.match( /Android/ ) && ! navigator.userAgent.match( /Android 3/) ) {
window.location="mobile.html";
}
else if ( navigator.userAgent.match( /Windows Phone/ ) || navigator.userAgent.match( /Zune/ ) ) {
window.location="mobile.html";
}
一切正常,直到我们在 IE9 上测试它,由于某种原因重定向到移动站点,即使它的 userAgent 不包含上述任何字符串。 IE9 的 userAgent 是:
Mozilla/5.0(兼容;MSIE 9.0;Windows NT 6.1;WOW64;Trident/5.0)
IE8 没有这种行为方式,任何其他平台也没有。是脚本不正确,还是 IE 再次因复仇而失败?谢谢。
【问题讨论】:
-
如果你通过代码调试,你能看出它落入了哪一个吗?
-
@RMX - 我同意亚当的观点。尝试在每个控制结构中发出
console.log(navigator.userAgent);以确定匹配的条件以及报告的 userAgent 究竟是什么。
标签: javascript html internet-explorer