在我们的索引页面上,我们有脚本来将智能手机(iPhone、安卓和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 (兼容;MSIE9.0;Windows NT 6.1;WOW64;三叉树/5.0)
IE8不是这样的,其他任何平台也不是这样。是脚本不正确,还是IE用它的报复性骗局再次击倒了?谢谢。
发布于 2012-05-08 10:49:20
根据此post regarding IE9 User Agents,IE9会在兼容模式下更改其用户代理,并且可以包含'Zune‘字符串。看起来我们必须尝试使用另一个字符串来重定向Windows Phone用户。谢谢。
发布于 2012-05-07 08:14:52
当您使用.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()方法将只返回true或false
发布于 2014-02-16 06:41:21
感谢您的回答和帮助。以下内容适用于我使用Windows 8手机的情况
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/mobilehttps://stackoverflow.com/questions/10452117
复制相似问题