Android 是否支持 window.location.replace 或任何等效项? [英] Does Android support window.location.replace or any equivalent?
问题描述
Android 浏览器似乎没有正确实现window.location.replace
.
It seems that the Android browser doesn't properly implement window.location.replace
.
在大多数浏览器中,调用 window.location.replace
会将当前 URL 替换为传递给它的 URL.
In most browsers, calling window.location.replace
will replace the current URL with the URL passed to it.
当用户导航到其他地方然后点击返回时,他们将返回到传递给 window.location.replace
的 URL,而不是他们在 之前所在的 URLwindow.location.replace
被调用.
When the user navigates somewhere else then clicks back, they'll be returned to the URL that was passed to window.location.replace
, rather than the URL that they were at before window.location.replace
was called.
Android 浏览器似乎没有正确实现这一点.
The Android browser doesn't seem to implement this properly.
在 Android 浏览器中,用户将被引导回原始 URL,而不是传递给 window.location.replace
的 URL.
In the Android browser, the user will be directed back to the original URL rather than the one passed to window.location.replace
.
您可以在此处亲自测试.
那么有没有其他方法可以在 Android 中重写历史记录?或者,对于 Android 用户,我将不得不在没有该功能的情况下生活?
So is there any alternative way to re-write history in Android? Or will I just have to live without that feature, for Android users?
推荐答案
我遇到了同样的问题,最终得到了类似于 chris 建议的代码,但我更改了 if 语句以使用 Modernizr 的功能检测.如果您不使用 Modernizr,代码将如下所示:
I had the same issue and ended up with code similar to chris suggestion, but I changed the if statement to use modernizr's feature detection. If you're not using modernizr the code would look something like this:
if(!!(window.history && history.replaceState)){
window.history.replaceState({}, document.title, base + fragment);
} else {
location.replace(base + fragment);
}
除非您有特定的设备检测原因,否则首选功能检测,因为它基本上支持所有设备,甚至是未来的设备.
Unless you have a specific reason for device detection, feature detection is preferred since it basically supports all devices, even future ones.
这篇关于Android 是否支持 window.location.replace 或任何等效项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!