简体   繁体   English

如何在javascript或jquery中跟踪浏览器刷新/返回/选项卡关闭和浏览器关闭事件

[英]How to track browser refresh/back/tab close and browser close event in javascript or jquery

I am using onbeforeunload function.我正在使用 onbeforeunload 功能。 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 .当任何人单击浏览器后退按钮、浏览器刷新按钮、浏览器选项卡关闭按钮和浏览器关闭按钮时,我想提醒不同的消息。那么我如何跟踪 onbeforeUnload 函数中的所有事件。

My code structure is like that我的代码结构是这样的

<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.出于安全原因,大多数浏览器都不允许这些事情。

You can, however, catch a few things.但是,您可以捕捉到一些东西。 Like keydown on the f5 key.就像f5键上的keydown That way you can do some stuff there before the "onbeforeunload" function runs if you like.这样你就可以在“onbeforeunload”函数运行之前在那里做一些事情,如果你愿意的话。

But you can't bind an event on the "back" button for instance.但是,例如,您不能在“后退”按钮上绑定事件。 Or "ctrl+r".或者“ctrl+r”。

So I'm afraid you'll have to reconsider your options, and go with some other solution.因此,恐怕您将不得不重新考虑您的选择,并采用其他解决方案。

Thankyou so much for reply.But i have found some code非常感谢您的回复。但我找到了一些代码

 if(window.event){
               if (window.event.clientX < 40 && window.event.clientY < 0)
                 {
                    alert("Backbutton is clicked");
                 }
                 else
                 { 
                  return exitPage(); 
                }
            }
    else{

      if(event.currentTarget.performance.navigation.type == 2)
      {
          alert("Back button click in mozilla");
      }
      if(event.currentTarget.performance.navigation.type == 1)
      {
        return exitPage();
      }
}

Using this code i can track the back button event but it only working in IE使用此代码我可以跟踪后退按钮事件,但它只能在 IE 中工作

you can do some small things before the customer closes the tab.您可以在客户关闭选项卡之前做一些小事。 javascript detect browser close tab/close browser but if your list of actions are big and the tab closes before it is finished you are helpless. javascript 检测浏览器关闭选项卡/关闭浏览器,但如果您的操作列表很大并且选项卡在完成之前关闭,您将无能为力。 You can try it but with my experience donot depend on it.你可以试试,但根据我的经验,不要依赖它。

window.addEventListener("beforeunload", function (e) {
  var confirmationMessage = "\o/";
  /* Do you small action code here */
  (e || window.event).returnValue = confirmationMessage; //Gecko + IE
  return confirmationMessage;                            //Webkit, Safari, Chrome
});

https://developer.mozilla.org/en-US/docs/Web/Reference/Events/beforeunload?redirectlocale=en-US&redirectslug=DOM/Mozilla_event_reference/beforeunload https://developer.mozilla.org/en-US/docs/Web/Reference/Events/beforeunload?redirectlocale=en-US&redirectslug=DOM/Mozilla_event_reference/beforeunload

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM