【问题标题】:IE9 erroneously redirecting to mobile siteIE9 错误地重定向到移动站点
【发布时间】: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


【解决方案1】:

根据post regarding IE9 User Agents,IE9 在兼容模式下会更改其用户代理,并且可以包含“Zune”字符串。看起来我们将不得不尝试使用另一个字符串来重定向 Windows Phone 用户。谢谢。

【讨论】:

  • 你不需要。只要确保您指定了正确的文档类型,IE9 就不会进入兼容模式。
【解决方案2】:

当你使用 .match() 方法时,它返回数组,可以为空([]),也可以不为空。
IE 可能认为[]true 表达式,所以我建议您执行以下操作:

if(/iPhone/.test(navigator.userAgent) && !/iPad/.test(navigator.userAgent)){
    window.location="mobile.html";
}else if(/Android/.test(navigator.userAgent) && !/Android 3/.test(navigator.userAgent){
    window.location="mobile.html";
}else if(/Windows Phone/.test(navigator.userAgent) || /Zune/.test(navigator.userAgent)){
    window.location="mobile.html";
}

我猜它会起作用,因为 .test() 方法只会返回 truefalse

【讨论】:

  • IE9 仍在使用您的脚本重定向到移动站点。我们删除了脚本的最后一行(带有“Windows Phone”和“Zune”的那个),现在 IE9 不会重定向。还要确保 IE9 开发人员工具中的 userAgent 被选择为默认值,并且没有更改为其他任何内容。
【解决方案3】:

感谢您的回答和帮助。以下对我使用 Windows 8 Phone 有用

if(/iPhone/.test(navigator.userAgent) && !/iPad/.test(navigator.userAgent)){
    window.location="/mobile";
}else if(/Android/.test(navigator.userAgent) && !/Android 3/.test(navigator.userAgent){
    window.location="/mobile";
}else if(/Windows Phone/.test(navigator.userAgent) || /Zune/.test(navigator.userAgent)){
    window.location="/mobile";
}

我将 window.location="mobile.html" 更改为我为移动设备的 index.html 文件创建的实际目录

ex. /root directory/mobile

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多