Opera 中的 Javascript location.replace 错误
我正在 Opera 中做一个 JS location.replace 。有一个已知错误,即仅当 location.hash 更改时,位置不会被替换而是更新(请参阅 http://my.opera.com/community/forums/topic.dml?id=568931)。
我试图执行以下解决方法:
var url = location.href.split("#")[0];
if (window.opera) {
window.history.back();
}
location.replace(url + '#' + newhash);
不幸的是,这似乎不起作用。在开始尝试 setTimeout 之前,我想检查一下是否有人有更好的主意。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为最好的解决方法是根本不解决它。
推理:首先,如果我使用后退按钮,或者调用
history.back()
,则在 this 页面中运行的脚本应该终止。因此,在上面的解决方法中,脚本实际上将(或应该)在location.replace()
调用之前停止运行。我们不记得您想要调用 location.replace() 并在您返回的页面上执行此操作,因为这将是脚本注入安全问题。其次,即使这个解决方法有效,我也强烈建议不要使用它。原因是 Opera 最终会修复它的 bug。如果最终用户使用固定 Opera 版本和运行您的脚本的页面,则每次单击您的链接之一都会从该用户的浏览历史记录中删除一个条目。
要获得正确的解决方案,您可以调查 < code>history.replaceState() - HTML5 中指定的新方法: http://www.whatwg.org/specs/web-apps/current-work/multipage/history.html#dom-history-replacestate
I think the best workaround for this is to not work around it at all.
Reasoning: firstly, the script running in this page should be terminated if I use the back button, or
history.back()
is called. Hence, in your workaround above the script will (or should) actually stop running before thelocation.replace()
call. We can not remember that you wanted to call location.replace() and do it on the page you've gone back to, because that would be a script injection security issue.Secondly, even if this workaround worked I would very much recommend not using it. The reason is that Opera will eventually fix its bug. If an end user used a fixed Opera version and a page running your script, each click on one of your links would remove one entry from that user's browsing history..
For a proper solution, you could investigate
history.replaceState()
- a new method specified in HTML5: http://www.whatwg.org/specs/web-apps/current-work/multipage/history.html#dom-history-replacestate你能澄清一下吗?我从您发布的论坛链接中获取了示例并将其上传到此处:http://people。 opera.com/miket/tmp/replace.html。在 Opera 11.61/Mac 中,它似乎按预期工作。
你看到了什么不同的东西吗?您能更详细地解释一下您的问题吗?
Can you clarify a bit? I took the example from the forum link you posted and uploaded it here: http://people.opera.com/miket/tmp/replace.html. In Opera 11.61/Mac, it appears to work as expected.
Are you seeing something different? Can you explain your problem in more detail?