为 Ajax 应用程序启用后退/前进键事件
我有一个主要在 AJAX 上运行的应用程序。 不过我想要其中有导航功能。 为了欺骗 URL,我更改了 location.hash 以生成 URL。 但如果我使用 back/fwd,只有 url 发生变化,但页面不会重新加载。 如何覆盖 hstory.back
以重新加载页面。
I have an application which works heavily on AJAX. However I want to have navigation functionalities in it. To spoof the url, I am changing the location.hash, to generate URL. But if I use back/fwd, only the url changes, but page wont reload. How can I override the hstory.back
to reload the page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除了连续轮询之外,我不知道还有什么其他方法可以实现这种行为。 一个实现可能如下所示:
I don't know of any other way than continuous polling to implement this behaviour. An implementation might look like this:
您无法准确捕获后面的事件,但大多数问题都已得到解决 - 这也是一件好事,这是一个困难问题。
查看真正简单的历史记录(又名 RSH),然后实施它或解决它看看它是如何工作的。
You can't exactly capture the back event, but most of these problems have been solved - and a good thing too, it's a hard problem.
Take a look at really simple history (aka RSH) and either implement it or work through it to see how it works.
这个问题的答案或多或少与我对这些问题的答案相同:
总之,您可能想要查看的两个项目解释了整个 hashchange 过程并将其与 ajax 结合使用:
jQuery Ajaxy(jQuery History 的 ajax 扩展,允许完整的 ajax 网站同时完全不引人注目且可优雅地降解)。
The answer for this question will be more or less the same as my answers for these questions:
In summary, two projects that you'll probably want to look at which explain the whole hashchange process and using it with ajax are:
jQuery History (using hashes to manage your pages state and bind to changes to update your page).
jQuery Ajaxy (ajax extension for jQuery History, to allow for complete ajax websites while being completely unobtrusive and gracefully degradable).
balupton 的答案真的很棒。
但是你还有另一个 jQuery 插件来处理你的 ajax 请求,它是 address。
The balupton answers are really great.
But you also have another jQuery Plugin to handle your ajax requests, it is address.