为什么onbeforeunload()执行当页面刷新时? [英] Why onbeforeunload() executeing When Page get refreshed?

查看:176
本文介绍了为什么onbeforeunload()执行当页面刷新时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我已经为浏览器关闭编写了代码。关闭浏览器选项卡后,将删除所有会话。但是当我刷新所有会话正在删除的页面时,我遇到了一个问题。我的代码在下面

Hi Guys,
I have written code for browser closed. When Browser tab is closed all sessions will be removed. But I am facing a problem when I am refreshing the page all session are removing. My code is below

<script language="javascript" type="text/javascript">
     var isClose = false;

     //this code will handle the F5 or Ctrl+F5 key 
     //need to handle more cases like ctrl+R whose codes are not listed here 
     document.onkeydown = checkKeycode
     function checkKeycode(e) {
         var keycode;
         if (window.event)
             keycode = window.event.keyCode;
         else if (e)
             keycode = e.which;
         if (keycode == 116) {
             isClose = true;
         }
     }
     function somefunction() {
         isClose = true;
     }





//并在身体标签中调用所有功能

< body>



请帮忙,为什么会这样?我希望清除关闭浏览器的会话不会在页面被推荐时。



感谢和问候

Amar



//and calling all function in body tag
<body >

Please help, Why is that happening? I want to clear session on browser closed Not when page get referesed.

Thank and regards
Amar

推荐答案

当发生以下任何一种情况时, beforeunload 事件被明确记录为触发:

The beforeunload event is clearly documented as firing when any of the following happen:



  • 关闭当前窗口。
  • 通过输入新地址或选择收藏夹导航到其他位置。
  • 单击引用其他文档的锚点。
  • 调用 anchor.click 方法。
  • 调用 document.write 方法。
  • 调用 document.close 方法。
  • 调用 window.close 方法。
  • 调用 window.navigate NavigateAndFind 方法。
  • 调用 location.replace 方法。
  • 调用 location.reload 方法。
  • location.href 属性指定新值。
  • 通过 INPUT type = submit ACTION 属性中指定的地址>控制,或调用 form.submit m ethod。
  • 调用 window.open 方法,为窗口提供可能的值 _self 名称。
  • 调用 document.open 方法。
  • 单击后退,前进,刷新或主页按钮。

    • Close the current window.
    • Navigate to another location by entering a new address or selecting a Favorite.
    • Click an anchor that refers to another document.
    • Invoke the anchor.click method.
    • Invoke the document.write method.
    • Invoke the document.close method.
    • Invoke the window.close method.
    • Invoke the window.navigate or NavigateAndFind method.
    • Invoke the location.replace method.
    • Invoke the location.reload method.
    • Specify a new value for the location.href property.
    • Submit a form to the address specified in the ACTION attribute via the INPUT type=submit control, or invoke the form.submit method.
    • Invoke the window.open method, providing the possible value _self for the window name.
    • Invoke the document.open method.
    • Click the Back, Forward, Refresh, or Home button.

    • beforeunload的工作方式,你无法区分天气,它是页面刷新或浏览器关闭。



      The way beforeunload works, you can not differentiate weather it's a page refresh or a browser close.

      Quote:

      当窗口,文档及其资源是即将被卸载。

      The beforeunload event is fired when the window, the document and its resources are about to be unloaded.





      https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload [ ^ ]



      但是,根据您的要求,您可以使用cookie或本地存储,以了解它是否是一个pa ge刷新或关闭。



      在这里看到类似的讨论:



      http://stackoverflow.com/questions/17026464/differentiate-browser-refresh-and-browser-close [ ^ ]



      http://stackoverflow.com/questions/ 568977 /识别 - 刷新和关闭浏览器操作 [ ^ ]



      https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload[^]

      However, for your requirement you could use a cookie or a local storage to know if it was a page refresh or close.

      See similar dicussion here:

      http://stackoverflow.com/questions/17026464/differentiate-browser-refresh-and-browser-close[^]

      http://stackoverflow.com/questions/568977/identifying-between-refresh-and-close-browser-actions[^]


      这篇关于为什么onbeforeunload()执行当页面刷新时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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