【问题标题】:Does Android support window.location.replace or any equivalent?Android 是否支持 window.location.replace 或任何等价物?
【发布时间】:2013-02-18 01:22:19
【问题描述】:

Android 浏览器似乎没有正确实现window.location.replace

在大多数浏览器中,调用 window.location.replace 会将当前 URL 替换为传递给它的 URL。

当用户导航到其他地方然后点击返回时,他们将返回到传递给 window.location.replace 的 URL,而不是他们在调用 window.location.replace 之前所在的 URL。

Android 浏览器似乎没有正确实现这一点。

在 Android 浏览器中,用户将被引导回原始 URL,而不是传递给 window.location.replace 的那个。

你可以自己测试一下here

那么有没有其他方法可以在 Android 中重写历史记录?或者,对于 Android 用户,我是否只能在没有该功能的情况下生活?

【问题讨论】:

  • 试试 document.location
  • @jonathanconway 你得到答案了吗?在我的情况下, location.replace 不会删除以前的 URL。如果你得到了答案,那么请提出建议。

标签: android mobile browser browser-history window.location


【解决方案1】:

我遇到了同样的问题,最终得到了类似于 chris 建议的代码,但我更改了 if 语句以使用 modernizr 的功能检测。 如果您不使用modernizr,代码将如下所示:

if(!!(window.history && history.replaceState)){
   window.history.replaceState({}, document.title, base + fragment);
} else {
   location.replace(base + fragment);
}

除非您有特定的设备检测原因,否则首选特征检测,因为它基本上支持所有设备,甚至未来的设备。

【讨论】:

  • 我不懂“基础”和“片段”。能解释清楚点吗?
  • 我使用了下面 Chris 示例中的 base + fragment。但它只是一个带有要添加到历史记录的 url(相对或绝对)的字符串。
【解决方案2】:

要使其适用于所有/大多数移动平台,请查看此link

展示如何处理 Android、iPad 和 iPhone 的重定向。

Android 使用 document.location 而 iOS 支持 window.location.replace

【讨论】:

  • 也就是说,Android 不支持在没有历史记录的情况下更改 URL 的任何方式?
  • 我不确定,试试this 看看是否有帮助
  • 您发送的链接没有解决问题。是的,它重定向用户并将重定向 URL 保留在历史记录中。但原始 URL 也保留在历史中。而我想将原始 URL 从历史中彻底清除。看起来这在 Android 上是不可能的。 :(
【解决方案3】:

这行得通吗?

document.location.href = 'http://example.com/somePage.html';

【讨论】:

  • 如果我只想更改 URL,这将起作用,但如果我想将以前的 URL 保留在浏览器历史记录之外,它将不起作用。上述解决方案会将之前的 URL 保留在历史记录中。
【解决方案4】:

你可以尝试在历史window.history上使用replaceState方法

      if (((navigator.userAgent.toLowerCase().indexOf('mozilla/5.0') > -1 && navigator.userAgent.toLowerCase().indexOf('android ') > -1 && navigator.userAgent.toLowerCase().indexOf('applewebkit') > -1) && !(navigator.userAgent.toLowerCase().indexOf('chrome') > -1))) {
          window.history.replaceState({}, document.title, base + fragment);
      } else {
          location.replace(base + fragment);
      }

【讨论】:

    猜你喜欢
    • 2015-05-19
    • 1970-01-01
    • 2015-11-03
    • 2014-02-26
    • 2018-07-06
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多