Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...