管理浏览器历史记录的巧妙方法

发布于 2024-10-02 12:54:19 字数 406 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请爱~陌生人 2024-10-09 12:54:19

有“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.

梦罢 2024-10-09 12:54:19

也许是一本有趣的读物,它是针对您的具体问题的现成解决方案正在做。
不,在每个浏览器都正确支持 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

时间你老了 2024-10-09 12:54:19

使用这个插件: 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:

  • Bookmarking in a browser or social website
  • Sending links via email or instant messenger
  • Finding specific content using the major search engines
  • Utilizing browser history and reload buttons
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文