簡體   English   中英

將桌面用戶重定向到另一個頁面,將移動用戶重定向到主頁面

[英]redirect desktop users to another page and mobile user to the main page

我有這個代碼的問題。我想將我的桌面用戶重定向到block.php頁面,不要讓他們進入。

我測試了很多代碼,但沒有為我工作。

此刻,我發現這個代碼。它運作完美......但是存在一個小問題。

當我用手機(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> 

問題是什么?

假設此腳本在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"); } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM