分配给document.location.href而不破坏历史记录 [英] Assigning to document.location.href without clobbering history
问题描述
在测试document.location.href时,我发现当用户启动导致javascript分配给document.location.href的操作时,新的URL会被添加到历史记录中。
In testing document.location.href, I have observed that when the user initiates an action that results in javascript that assigns to document.location.href, the new URL is added to the history.
但是,如果通过javascript启动调用,例如,XMLHTTPRequest的状态更改,则会覆盖历史记录中当前页面的条目。我有正确的特征吗?在后一种情况下,有没有办法让页面更改反映在历史记录中?
However, if the call is initiated by javascript that is result of, say, state change of an XMLHTTPRequest, the entry for the current page in the history is over-written. Have I characterized this correctly? Is there a way to get the page change to be reflected in the history in this latter case?
推荐答案
我面对同样的问题问题,发现这个解决方法对我有用
I was facing the same problem and found this workaround which worked for me
而不是
function onAjaxCallback(evt){
location.href=newLocation;
}
我围绕setTimeout包装了location.href调用。似乎可以做到这一点。我的历史现在表现得很好。希望有帮助
i wrapped the location.href call around a setTimeout. Seems to do the trick. My history's behaving fine now. Hope that helps
function onAjaxCallback(evt){
setTimeout(function(){
location.href=newLocation;
},0)
}
这篇关于分配给document.location.href而不破坏历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!