javascript - 将桌面用户重定向到另一个页面,将移动用户重定向到主页

标签 javascript html ios redirect

我的这段代码有问题。我想将我的桌面用户重定向到 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 block 内调用 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"); 
}

关于javascript - 将桌面用户重定向到另一个页面,将移动用户重定向到主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40844657/

相关文章:

javascript - 根据jquery中另一个复选框的选择禁用复选框

javascript - IE11 在基于 Kendo/JavaScript 的 Web 应用程序中崩溃

javascript - css 样式表没有被添加

html - center 标签工作正常,但 align 属性不行!可能是什么问题?

javascript - RequireJS 和 Backbone 入门

javascript - Angular js typescript 中的 Http promise

html - react : Action on variable change

ios - 检查倒计时是否已为零并更新 UILabel

iphone - 这个 UI 元素是什么?

ios - iOS 13 应用程序的 "Enable Multiple Windows"设置(仅限 iPhone)