当用户单击同一链接时,Hashchange 不会触发

发布于 2024-12-23 08:21:16 字数 522 浏览 4 评论 0原文

我正在创建一个 HTML 和 Javascript 客户端,以便在与 REST API 通信的浏览器中运行。我正在使用 RouteMap 来设置我的网址。所以我保持了这样的约定,

http://mysite.com/#/{ResourceName}/[edit|view|list]/[Id]/

我只设置了一条路线,然后在绑定到 hashchange 的函数中获取这些参数。大多数事情都运行良好。只有两个问题,我因此陷入困境。

  1. 如果用户单击同一链接两次,则不会触发 hashchange 事件。是的,哈希值没有改变,所以显然它不会触发。但应该有一些事情可以做,但我错过了。
  2. 如果我更改了 UI 中的某些内容(例如调出新的 div 并隐藏了一些内容),而我不想更改其哈希链接,则我会丢失该历史记录,并且无法通过正确单击后退按钮返回。

任何帮助将不胜感激。

I'm creating an HTML and Javascript client for running in browser which talks to REST API. I'm using RouteMap to set my URLs. So I've kept a convention something like this

http://mysite.com/#/{ResourceName}/[edit|view|list]/[Id]/

I've set just one route and I'm grabbing these parameters in the function bounded to hashchange. Most of the things work fine. Just two issues and I'm stuck because of them.

  1. If the user clicks on the same link twice, hashchange event doesn't fire. Yes, hash has not changed so obviously it won't fire. But there should be something which can be done and I'm missing that.
  2. If I change something in the UI (like bring up new divs and hide some) for which I don't want to change the hash link, I loose that history and can't go back by clicking the back button properly.

Any help will be grateful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夜雨飘雪 2024-12-30 08:21:16

对于#1,您可能希望将处理程序附加到链接单击事件。这样您就可以判断链接是否被单击。当我使用 onhashchange 时,我总是在点击事件中附加一些东西来协助 onhashchange 的 polyfill,所以至少我可以知道它何时失败。

对于#2,我想指出自动更改用户历史记录是有问题的。你可以用微小的、无意义的哈希更改来填充某人的历史记录。我建议仅在用户实际交互时更改历史记录。除此之外,HTML5 确实提供了 pushStatepopState参考

For #1, you probably want to attach a handler to the link click event. That way you can tell if the link is being clicked. When I use onhashchange, I always attach something to the click event to assist polyfills for onhashchange, so at least I can tell when it's failing.

For #2, I want to point out that having automatic stuff change the user's history is problematic. You could fill someone's history with minute, meaningless hash changes. I recommend only changing the history when the user actually interacts. Short of that, HTML5 does offer pushState and popState. Reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文