管理浏览器历史记录的巧妙方法
我正在尝试在 AJAX 应用程序中实现一个不错的导航。我现在正在做的事情如下:
- 每次用户单击 AJAX 链接时,都会执行相应的调用并更改哈希值。
- 每当加载新页面时,我都会检查哈希是否存在并进行相应的 AJAX 调用来更新页面。这可确保书签按预期工作。
- 每 500 毫秒我检查一次哈希值是否发生更改并执行相应的 AJAX 调用。这可以照顾用户按下后退/前进按钮,尽管有轻微的延迟。
第三点有点烦人:我不想每 500 毫秒就有一个超时来检查哈希值,因为大多数时候它会保持不变。
有没有更好的方法来管理这个问题?我想不出任何替代方案,但也许我错过了一些东西。
请不要向我指出现成的解决方案,除非您知道它们基于不同的机制。
I'm trying to implement a decent navigation in a AJAX application. What I am doing right now is the following:
- Every time a user clicks an AJAX link, the corresponding call is performed and the hash is changed.
- Whenever a new page is loaded, I check whether the hash is present and make the corresponding AJAX call to update the page. This ensures that bookmarks will work as expected.
- Every 500ms I check if the hash is changed and perform the corresponding AJAX call. This takes care of users pressing the back/forward buttons, albeit with a sligth delay.
The third point is a bit annoying: I'd rather not have a timeout every 500ms just to check the hash, as most of the time it will stay the same.
Is there a better way to manage this? I cannot think of any alternatives, but maybe I am missing something.
Please, do not point me towards ready-made solutions, unless you know they are based on a different mechanism.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有“hashchange”事件,该事件将在 HTML5 中实现。我不确定现在的支持有多好...... IE8 支持它,我认为 Mozilla 在最近的版本中有自己的实现。除此之外,我没有什么害怕的。检查 exery x ms 是每个人都会这样做的方式。
There is the "hashchange" event, which is to be implemented in HTML5. I'm not sure how good support is now... IE8 supports it, and I think Mozilla have their own implementation in a recent release. Other than that, there is nothing I'm afraid. Checking exery x ms is the way everyone does it.
也许这是一本有趣的读物,它是针对您的具体问题的现成解决方案正在做。
不,在每个浏览器都正确支持 hashchange 之前(阅读:ie),您必须手动检查
maybe this is an interesting read, it is a ready made solution for the exact thing you are doing.
and no, until the hashchange is properly supported in every browser (read: ie), you will have to check manually
使用这个插件: http://www.asual.com/jquery/address/
jQuery地址插件提供强大的深度链接功能,并允许创建可以指向网站部分或应用程序状态的唯一虚拟地址。它支持许多重要的功能,包括:
Use this plugin: http://www.asual.com/jquery/address/
The jQuery Address plugin provides powerful deep linking capabilities and allows the creation of unique virtual addresses that can point to a website section or an application state. It enables a number of important capabilities including: