2016-11-28 139 views
1

我有一个问题,这个code.I要重定向我的桌面用户block.php页面,不要让他们进来。重定向桌面用户到另一个网页和移动用户主页

我测试了很多的代码和非他们为我工作。

此刻,我发现这code.its作品完美...但有一个小问题。

当我检查我的手机(iOS设备)的网站。它保持重装

这是代码:

<script> 
 

 
if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i)) { 
 
    window.location.assign("http://example.com"); 
 
} else { 
 
    window.location.assign("http://example.com/block.php"); 
 
} 
 

 
</script>

是什么问题?

+0

你有尝试的.htaccess? – keronconk

+0

@keronconk no..i想使用JS ...但是它如何与.htaccess? –

回答

0

假设此脚本在http://example.com上运行,调用if块内的window.location.assign将重新加载页面。在我看来,只有当用户不在移动设备上时,才想调用window.location.assign('http://example.com/block.php')。

你可以尝试这样的事:

function checkIsMobile() { 
 
    if(navigator.userAgent.match(/iPhone/i)){ 
 
    return true; 
 
    } else if (navigator.userAgent.match(/iPad/i)){ 
 
     return true; 
 
    } else if (navigator.userAgent.match(/iPod/i)){ 
 
     return true; 
 
    } else { 
 
     return false; 
 
    } 
 

 
    } 
 

 
if (!checkIsMobile()) { 
 
    window.location.assign("http://example.com/block.php"); 
 
}

+0

谢谢你...现在它可以工作...但它可以欺骗用户代理切换器扩展在Firefox或铬..有没有解决方案? –

+0

大多数人推荐使用功能检测来代替UA嗅探,因为您很容易就会欺骗该信息。即使使用功能检测,由于设备生态系统非常广泛,因此这类任务不会100%准确。看看这篇关于功能检测的文章,了解更多信息:https://www.html5rocks.com/en/tutorials/detection/ –

+0

谢谢,但链接中断 –

相关问题