1

Hey everyone, this code is meant to read what comes after the hash in the url and display a specific div depending on that value. Right now it works fine, except for when you try changing the hash while you're already on the site (or if you hit the back button, which has the same effect: changes the hash, but the div isnt changed). The site is made so that it doesnt reload with every div change (the main content of the page), so I'm assuming the reason the div doesnt change when you hit the back button or type in a different value after the hash is because the page is already loaded, and the javascript code is only called once on page load. I need a way for it to be called whenever the url is changed. Here's the code:

function hash() { var hash = window.location.hash.replace('#',''); InsertContent(hash); }

4

3 回答 3

0

您需要轮询 window.location.hash 并查看它是否更改。一些浏览器支持onhashchange事件。

另一个讨论:关于 - window.location.hash - 改变?

于 2010-11-18T23:40:05.840 回答
0

恐怕这不是一个错误,这是一个功能。例如,Opera 将页面的确切状态存储在内存中。因此,如果您点击后退按钮,该状态将完全恢复。就像您从未更改过页面一样。

但是,通过在更改哈希后立即调用来更改哈希应该可以工作InsertContent()。那不行吗?你能给我们看一下代码吗?

于 2010-11-18T23:35:37.950 回答
0

jQuery 有一个名为“address”的插件来解决这个问题

于 2010-11-19T01:47:15.157 回答