如何在javascript中捕获浏览器的后退/前进按钮单击事件或哈希更改事件?
我想在单击浏览器的后退或前进按钮或在 JavaScript 中更改哈希值时发出 alert()
。我已经尝试过 这个 解决方案并且它正在工作,但它导致网页中的其他链接出现问题,并且在任何链接点击事件上提交每个请求两次。
有没有解决方案可以在不使用 setInterval() 函数的情况下捕获它?所以我需要捕获哈希更改或后退/前进按钮点击事件?我需要一个简单的 javascript 代码/函数/属性,它应该适用于所有现代浏览器。
有什么解决办法吗?
谢谢
I want to alert()
when browser's back or forward button is clicked or hash is changed in javascript. I have tried this solution and it is working but it is causing problems on other links in webpage and submit each request twice on any link click event.
Is there any solution to capture it without using setInterval()
function? So I need to capture hash change or back/forward button click event? I need a simple javascript code/function/property that should work in all modern browsers.
Any solution ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不是一个好主意,
您能解释一下这背后的原因吗?我们都一直在防止后退/前进以及类似和破坏浏览器功能的道路上走下去。
事实证明,最好遵守浏览器并以这种方式编写应用程序,这样这些事情就变得无关紧要了。而且浏览器确实将越来越多的东西锁定到客户端 JavaScript 应用程序,因此在(几次)浏览器升级后您的应用程序很可能会失败。
使用 HTML5
HTML5 历史规范 可能正是您所追求的。这是 Ajax 应用程序和浏览器后退/前进功能的工作和完成方式。我建议你检查一下。请参阅工作演示,它做得相当好。
Not a good idea
Can you rather explain the reasoning behind this? We've all been down this road of preventing backs/forwards and similar and mangling with browser functionality.
It turns out though it's better to obey to browser and write your application in that way so these things become irrelevant. And it's also true that browsers are locking more and more things to client javascript apps so it's highly likely your app is going to fail after (few) browser upgrades.
Go with HTML5
HTML5 History spec may be exactly what you're after. It's the way things should work and be done in regard to Ajax applications and browser0s back/forward functionality. I suggest you check it out. See a working demo that does this rather nicely.
我相信这就是 Robert Koritnik 正在寻找的答案,我在这里找到了它: https://developers.google.com/tv/web/articles/location-hash-navigation
只要位置哈希更新或更改,就会触发一个事件 (window.onhashchange),因此您只需执行以下操作即可做的是使用 JavaScript 设置事件处理程序来侦听此事件并根据哈希执行代码。这基本上是如何完成的:
我让它在我的网站上运行:
http://www.designhandler.com
都是动态变化的内容。还没有ajax,但当我完成后就会了。我仍然使用 window.location.hash 来跟踪站点状态。如果您浏览网站,然后在网站进入历史记录后开始使用后退按钮进行导航,它将动态更改状态,就像用户实际单击导航一样,而不需要随后重新加载页面。
I believe this is the answer Robert Koritnik was looking for, I found it here: https://developers.google.com/tv/web/articles/location-hash-navigation
There is an event (window.onhashchange) that fires whenever the location hash has been updated or changed so all you have to do is set up an event handler using JavaScript to listen for this event and execute code based on the hash. This is basically how it is done:
I have it working on my site:
http://www.designhandler.com
It is all dynamically changing content. No ajax yet but when I am finished it will be. I still use the window.location.hash to keep track of the site states. If you navigate through the site and then begin to use the back forward buttons to navigate once the site is in the history it will change the states dynamically like if the user was actually clicking through the nav, rather than needing to reload the page afterward.
这是为了哈希还是为了重定向?你想做什么?这种行为通常具有很强的侵入性。
您可能想在离开页面之前尝试此 javascript 的“onbeforeunload”事件
已编辑
实际上 ,您提供的链接非常准确。
您的链接重复操作问题将得到纠正:
如果您仅更改 By(更新哈希并让“事件”处理该操作),
It's this for hash or for redirection? What are you trying to do? This kind of action is usually highly intrusive.
You may want to try "onbeforeunload" event for this javascript before leaving the page
Edited
Actually, the link you provide is quite accurate.
Your link duplicate action problem would be corrected if you change
By just (update the hash and let the "event" handle the action) :
Javascript 提供了事件
popstate
来捕获浏览器的后退/前进按钮点击事件 -Javascript provide the event
popstate
to capture browser's back/forward button click event -