Does Android support window.location.replace or any equivalent?

风格不统一 提交于 2019-11-26 21:46:58

问题


It seems that the Android browser doesn't properly implement window.location.replace.

In most browsers, calling window.location.replace will replace the current URL with the URL passed to it.

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.

The Android browser doesn't seem to implement this properly.

In the Android browser, the user will be directed back to the original URL rather than the one passed to window.location.replace.

You can test this for yourself here.

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?


回答1:


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.




回答2:


To make it work across all/most mobile platforms check out this link.

Shows how to handle redirect for Android, iPad and iPhone.

Android uses document.location whereas iOS supports window.location.replace




回答3:


Will this work?

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



回答4:


You can try using the replaceState method on the history window.history

      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);
      }


来源:https://stackoverflow.com/questions/15193359/does-android-support-window-location-replace-or-any-equivalent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!