首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IE9错误地重定向到移动站点

IE9错误地重定向到移动站点
EN

Stack Overflow用户
提问于 2012-05-04 23:50:58
回答 3查看 676关注 0票数 2

在我们的索引页面上,我们有脚本来将智能手机(iPhone、安卓和Windows Phone)上的用户重定向到我们的移动网站。我们使用的技术是:

代码语言:javascript
复制
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 (兼容;MSIE9.0;Windows NT 6.1;WOW64;三叉树/5.0)

IE8不是这样的,其他任何平台也不是这样。是脚本不正确,还是IE用它的报复性骗局再次击倒了?谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-05-08 10:49:20

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

票数 2
EN

Stack Overflow用户

发布于 2012-05-07 08:14:52

当您使用.match()方法时,它返回数组,它可以为空([]),也可以不为空。

IE可能会考虑到[]是一个true表达式,所以我建议您执行以下操作:

代码语言:javascript
复制
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

票数 1
EN

Stack Overflow用户

发布于 2014-02-16 06:41:21

感谢您的回答和帮助。以下内容适用于我使用Windows 8手机的情况

代码语言:javascript
复制
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文件创建的实际目录

代码语言:javascript
复制
ex. /root directory/mobile
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10452117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档