我在网上做了一些窥探,发现window.location.hash = "etc"
是一种广泛采用的方法来更新浏览器的位置,而无需重新加载/刷新页面。我已经应用了这个例子,我已经熟了:http://dl.dropbox.com/u/1595444/locationExample/index.html
在Safari中运行良好,但是......
我注意到在更改hash
时Chrome 10+中的内容:
之前有没有人遇到这个问题?知道修复吗?
答案 0 :(得分:6)
这里最有可能发生两件事:
pushState
),网站图标和停止/刷新按钮会闪烁,但哈希更改是
在相同的代码路径上)。两者的解决方法是推迟更新哈希,直到用户完成滚动(您仍然可以立即更新当前项下显示的白条)。你可以通过以下方式来做到这一点:
var scrollTimeout;
window.onscroll = function() {
// update current item display here
if (scrollTimeout)
clearTimeout(scrollTimeout);
scrollTimeout = setTimeout(function() {
scrolTimeout = undefined;
// update hash here
}, 100);
};
由于看起来您正在使用jQuery,因此debouncing plugins可能会有所帮助。
答案 1 :(得分:1)
我没有明确的答案,但首先我会尝试:
答案 2 :(得分:1)
var r='#hello';
if(navigator.userAgent.indexOf('Chrome/')!=-1){
top.history.pushState("", "", r);
return;
};
if(r.charAt(0)=='/'){
top.location.replace(r);
}else{
top.location.hash=r;
};
为我工作。实际上我花了很长时间来弄明白这一点。 Firefox现在也支持history
对象,所以我们可以在几年内摆脱整个“哈希”事件。
编辑:是的,重新加载的东西是Chrome错误。