调用后我的后退按钮的行为有所不同window.location.replace(...)
,但我需要找到一种方法在所有浏览器(包括移动设备)上获得相同的行为。
给定以下 html(包含在文件 testpage.html 中):
<html>
<body>
<button onclick="window.location.replace('testpage.html#test2');">Replace</button>
<button onclick="window.location.assign('testpage.html#test3');">Assign</button>
</body>
</html>
最初的 url 是 testpage.html。
- 单击替换按钮 - URL 更改为 testpage.html#test2
- 单击分配按钮 - URL 更改为 testpge.html#test3
- 点击返回按钮...
然后不同的浏览器做不同的事情:
在 Chrome (21) 和 Firefox (15) 上,URL 变回 testpage.html#test2 并且无法再单击。这是我所期望的,这正是我想要的行为。
在 Safari (5) 和 iOS (5) 上,URL 变回 testpage.html(没有哈希片段),完全跳过了包含 #test2 的中间 URL。
在 IE (9) 和 WP7 上,url 完全跳回到我在 testpage.html 之前的位置
在 android 浏览器(姜饼和冰淇淋三明治)上,第一次单击替换按钮没有任何作用(行为不正确)。第二次单击分配按钮将 URL 设置为 testpage.html#test3,如预期的那样。然后后退按钮返回到 testpage.html 的 url,没有哈希片段,这与 Safari 和 iOS 相同,但在第一个按钮单击没有做任何事情的情况下,这似乎是正确的行为。问题是调用
replace(...)
不起作用。
无论如何,行为不同的原因是什么?有什么方法可以统一我在 Chrome 和 Firefox 上的行为。