jQuery:控制页面重新加载或离开当前页面
如果用户通过使用鼠标(单击链接)或键盘(在聚焦表单元素时按 Enter 键)交互离开当前页面,我希望能够事先运行 JavaScript/jQuery 命令。
示例工作流程:
- 单击任何链接或按下某个键,这将导致在浏览器中加载新页面
- JavaScript 事件被触发并执行某些操作(例如
console.log()
) - 用户 执行页面加载
此外,如果这也适用于触发页面重新加载的用户事件(例如按 F5),那就太好了。
我意识到 Stackoverflow 上也有类似的东西。如果答案框中有未保存的内容,它会在加载另一个页面之前显示警报。这可能是我要求的相同功能。
If a user moves away from the current page by using mouse (clicking on a link) or keyboard (hitting enter while having a form element focused) interactions, i want to do be able to run JavaScript/jQuery commands beforehand.
Exemplary workflow:
- User clicks on any link or hits a key which will result in loading a new page in the browser
- A JavaScript event gets fired and does something (for example
console.log()
) - Finally, the page load is executed
Furthermore, it would be great if that also works with user events that trigger a page reload (like pressing F5).
I realize that there is something similar here on Stackoverflow. It shows an alert before loading another page if there is unsaved content in an answer box. That may be the same functionality i ask for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
参考:JQuery unload()。
Reference: JQuery unload().