0
我在javascript中使用location.hash,以允许用户在ajax屏幕(在同一个html页面内动态添加和删除的div)之间切换。Location.hash导致无限循环
问题是,当我通过javascript更新location.hash时,窗口侦听器立即触发!我只需要在后退按钮实际点击时触发此事件,而不是在通过javascript更改历史记录时触发。
我的窗口侦听代码:
window.onhashchange = function() {
var s;
if (location.hash.length > 0) {
s = parseInt(location.hash.replace('#',''),10);
} else {
s = 1;
}
main.showScreen(s);
}
我的屏幕更新代码:
main.showScreen = function(i) {
// allow the back button to switch between screens
location.hash = i;
// but setting location.hash causes this same function to fire again!
//
// here follows the code that adds a new div with new text content
// ...
}
澄清:showScreen可以从应用程序的任何地方通过点击“下一个”被调用,例如按钮上的某个地方。