【问题标题】:window.location.assign on click not working in iOSwindow.location.assign 点击在 iOS 中不起作用
【发布时间】:2019-05-21 12:55:07
【问题描述】:

我有一个“点击时”window.location.assign 功能,它在我的桌面上运行良好,但在 iPhone 上却不行。每次我点击手机上的按钮时,我都会看到 URL 发生了变化,但随后又回到了原来的那个。

我尝试使用window.location.replace 和其他 JS 选项进行重定向,但我的手机上总是出现这个奇怪的“错误”。

    $('#redirect').on('click', function(){
      var url = "https://google.com/";
      window.location.assign(url);
      return false;
    });

为什么会发生这种情况以及如何解决?

【问题讨论】:

  • 我不知道为什么这不起作用,但不应该更改location.href 工作?
  • MDN seems to think it works on iOS#redirect 是按钮吗?
  • #redirect I 是链接还是按钮之类的?你并没有阻止默认,我敢打赌这个元素的原始操作(导致某种页面加载)发生在你的 JavaScript 之前。第二个 Safari 开始加载另一个页面,它冻结了页面。如果此元素本机执行某些操作,则必须 preventDefault() 。编辑:我刚刚看到返回错误。 preventDefault 会改变什么吗?
  • 我还认为 iOS 甚至可能不会触发该事件。见stackoverflow.com/questions/14795944/…stackoverflow.com/questions/3025348/…
  • @evolutionxbox 哦。是的,我可以看到。

标签: javascript ios redirect onclick url-redirection


【解决方案1】:

iOS 上的 Safari 很糟糕。如果您想检测 Safari 并使用可行的方法进行重定向,请尝试以下操作:

var url = "https://google.com/";
var uagent = navigator.userAgent.toLowerCase();
if(/safari/.test(uagent) && !/chrome/.test(uagent)) {
    window.location.href = url;
}

【讨论】:

  • “不好”是什么意思?你能解释一下为什么这个解决方案效果更好吗?
  • “很糟糕”我的意思是如果 Internet Explorer 有几个更新。
猜你喜欢
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 2021-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多