Javascript IE9错误地重定向到移动站点
在我们的索引页面上,我们有脚本将使用智能手机(iPhone、Android和Windows Phone)的用户重定向到我们的移动站点。我们使用的技术是:Javascript IE9错误地重定向到移动站点,javascript,html,internet-explorer,Javascript,Html,Internet Explorer,在我们的索引页面上,我们有脚本将使用智能手机(iPhone、Android和Windows Phone)的用户重定向到我们的移动站点。我们使用的技术是: if ( navigator.userAgent.match( /iPhone/ ) && ! navigator.userAgent.match( /iPad/ ) ) { window.location="mobile.html"; } else if ( navigator.user
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上测试之前,一切都运行得很好。由于某种原因,IE9重定向到移动站点,尽管它的userAgent不包含上述任何字符串。IE9的用户代理是:
Mozilla/5.0(兼容;MSIE 9.0;Windows NT 6.1;WOW64;Trident/5.0)
IE8不以这种方式运行,任何其他平台也不以这种方式运行。是剧本不正确,还是IE又一次用报复性的灌木丛击倒了它?谢谢。使用.match()方法时,它返回数组,数组可以为空([]
)也可以为空。IE很可能认为
[]
是一个正确的
表达式,因此我建议您执行以下操作:
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()方法只会返回
true
或false
,因为IE9在兼容模式下会更改其用户代理,并且可以包含“Zune”字符串。看来我们必须尝试使用另一个字符串重定向Windows Phone用户。谢谢。谢谢你的回答和帮助。以下内容适用于我使用Windows8手机的情况
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
如果你对代码进行调试,你能看到它落入了哪一个吗?@RMX-我同意亚当的观点。尝试发出
console.log(navigator.userAgent)
在每个控制结构中,以确定匹配的条件以及报告的userAgent的确切内容。IE9仍在使用脚本重定向到移动站点。我们删除了脚本的最后一行(一行w/‘Windows Phone’和‘Zune’),现在IE9没有重定向。还确保IE9开发者工具中的userAgent已选择为默认值,并且未更改为其他任何内容。您不需要这样做。只要确保指定了正确的doctype,IE9就不会进入兼容模式。