Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
JavaScript
<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;
     }


//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
Posted
Updated 17-Nov-14 2:27am
v2
Comments
ZurdoDev 17-Nov-14 8:00am    
First off, there is no guarantee you can always catch when the browser is closing so usually there is no point trying to figure that out. When the page refreshes, it closes the current page and reloads it which is causing it to fire.

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

  • 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.
 
Share this answer
 
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[^]

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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900