如何在javascript中捕获浏览器的后退/前进按钮单击事件或哈希更改事件? [英] How to capture browser's back/forward button click event or hash change event in javascript?

查看:16
本文介绍了如何在javascript中捕获浏览器的后退/前进按钮单击事件或哈希更改事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想 alert() 当浏览器的后退或前进按钮被点击或散列在 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.

有没有不使用 setInterval() 函数来捕获它的解决方案?所以我需要捕获hash changeback/forward 按钮点击事件?我需要一个简单的 javascript 代码/函数/属性,它应该适用于所有现代浏览器.

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.

有什么解决办法吗?

谢谢

推荐答案

不是一个好主意

你能解释一下这背后的原因吗?我们一直在这条道路上防止后退/前进以及类似和破坏浏览器功能.

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.

事实证明,虽然最好服从浏览器并以这种方式编写您的应用程序,以便这些事情变得无关紧要.而且,浏览器将越来越多的内容锁定到客户端 javascript 应用程序也是事实,因此您的应用程序很可能在(少数)浏览器升级后失败.

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.

HTML5 历史规范 可能正是您所追求的.这是有关 Ajax 应用程序和浏览器的后退/前进功能的工作和完成方式.我建议你检查一下.请参阅工作演示,该演示相当不错.

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.

这篇关于如何在javascript中捕获浏览器的后退/前进按钮单击事件或哈希更改事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆