History.back()不更新位置.在Chrome/FireFox中
history.back() does not update location.hash in Chrome/FireFox
我试图构建一个JS脚本,将改变页面的位置,回去,直到找到一个特定的哈希位置:
var StopAtThisHash ='#';
var CurrentHash = window.location.hash;
var continueLoop = true;
while ((window.history.length>0) && (continueLoop))
{
window.history.back();
var NowWeAreAtHash = window.location.hash; //this never changes in Chrome
//actually, always seems to: CurrentHash == NowWeAreAtHash;
if(NowWeAreAtHash == StopAtThisHash)
continueLoop= false;
}
奇怪的是,在Chrome和FF中,window.location.hash在back()之后没有改变。历史的长度也没有像我预期的那样减少1。循环无限运行,浏览器挂起。
在ie9中,这似乎按预期运行。
有什么解决方法吗?
function goBackHome () {
goBackToHash(''); // Assume the home is without hash. You can use '#'.
}
function goBackToHash(hash) {
setTimeout(function () {
if (window.location.hash == hash) return;
history.back();
goBackToHash(hash);
}, 0);
}
为了克服while
循环的问题,我尝试使用setTimeout
,但这通常比预期的要远…等待一个完美的解决方案。
我认为在history.back()
和window.location.hash
之间有延迟。
另一个解决方法是在JS中保留哈希值,以便您可以计算出history.go(N)
中需要多少步骤。
似乎window.history.back()
, window.history.forward()
和window.history.go()
不改变历史,只是来回导航。如果back()
会改变历史记录的长度,那么你就不能修改forward()
。
作为解决方案,我建议将window.history
与for
从最后到第一个循环,而不是while
。
相关文章:
- XMLHttpRequest - difference between Chrome & Firefox
- 长时间运行的脚本IE与Chrome/Firefox
- Javascript语法在Safari/Webkit中无效,在Chrome/Firefox中运行良好
- 代码在 Dreamweaver cs6 中工作,但在 chrome/firefox 中不起作用
- 在调试客户端时使用Chrome / Firefox
- Chrome/Firefox 使事件侦听器断点在停止之前跳过 jQuery 和其他库
- Chrome/Firefox 一次在所有 JavaScript onclick 事件上设置断点
- 未捕获的类型错误:无法读取IE和Chrome Firefox中未定义的属性“文档”很好
- 2D纹理渲染在Safari中闪烁,在Chrome / Firefox中很好
- 砌体JS插件 - Chrome / Firefox 问题
- Chrome/Firefox 中的 Rect 函数有什么用
- 在Chrome/Firefox中全屏按转义时未触发键下达事件
- 在PhantomJS中需要JS错误,但不是Chrome / FireFox
- Ruby Time to JavaScript Date in Chrome/Firefox
- 通过Google Chrome/Firefox Addon中的控制台访问/操作内容脚本
- 如何在Chrome/Firefox中导出ES6中的变量
- 在调用 window.location.asassigned 后,网页渲染在 chrome/firefox 之间的行为不
- 在将静态资源切换到CDN后,谷歌在Chrome+Firefox中映射错误,在Safari中很好:
- 粗体在Chrome/Firefox中太过粗体
- Chrome/Firefox中双美元符号选择器查询功能的来源是什么