如何在javascript或jquery中跟踪浏览器刷新/返回/制表符关闭和浏览器关闭事件 [英] How to track browser refresh/back/tab close and browser close event in javascript or jquery

查看:252
本文介绍了如何在javascript或jquery中跟踪浏览器刷新/返回/制表符关闭和浏览器关闭事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用onbeforeunload函数。我想在任何一次单击浏览器后退按钮,浏览器刷新按钮,浏览器选项卡关闭按钮和浏览器关闭按钮时提醒不同的消息。那么我如何跟踪onbeforeUnload函数内的所有事件。

I am using onbeforeunload function. I want to alert different message when any one click on browser back button , browser refresh button ,browser tab close button and browser close button .So how can i track all the events inside onbeforeUnload functions .

我的代码结构就像那样

<body  onbeforeunload="return closePage();">
<script type="text/javascript">    
function closePage() {
  if(back button click){
    alert("back button");
  } else if(refresh button click || f5){
    alert("refresh button click");
  } else if(browser tab close){
    alert("tab close");
  } else {
    alert("browser closed");
  }
}
</script>

有什么想法解决这个问题吗?非常感谢。

Any idea to fix this? Thanks a lot.

推荐答案

据我所知,你想要的是不可能的。出于安全考虑,大多数浏览器都不允许这类内容。

To the best of my knowledge, what you want is not possible. For security reasons most browsers won't allow these kinds of things.

但是,您可以捕捉到一些东西。喜欢 f5 键上的 keydown 。这样你可以在onbeforeunload函数运行之前做一些事情,如果你愿意的话。

You can, however, catch a few things. Like keydown on the f5 key. That way you can do some stuff there before the "onbeforeunload" function runs if you like.

但你不能在后退按钮上绑定一个事件,例如。或者ctrl + r。

But you can't bind an event on the "back" button for instance. Or "ctrl+r".

所以我担心你不得不重新考虑你的选择,并采取其他解决方案。

So I'm afraid you'll have to reconsider your options, and go with some other solution.

这篇关于如何在javascript或jquery中跟踪浏览器刷新/返回/制表符关闭和浏览器关闭事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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