2016-11-28 140 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

謝謝,但鏈接中斷 –

相關問題