2

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

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

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

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

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

你可以在这里自己测试一下。

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

4

4 回答 4

2

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

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

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

于 2014-05-01T05:52:08.253 回答
1

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

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

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

于 2013-03-04T02:04:01.933 回答
0

这行得通吗?

document.location.href = 'http://example.com/somePage.html';
于 2013-03-04T02:04:43.300 回答
0

您可以尝试在历史 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);
      }
于 2013-07-05T10:22:31.977 回答