0

I am developing a website. Where I need to logout the page when one try to refresh the page.

I got the code for logout when one click on f5 for refresh and also I got the code for disable right click on the page. but I dont know how to prevent the refresh button of browser or logout when I press refresh button. I searched many sites. But didn't get an answer.

The code I gor for F5 key is

function fkey(e){
    e = e || window.event;
   if( wasPressed ) return; 

    if (e.keyCode == 116) {
         alert("f5 pressed");
        wasPressed = true;
    }else {
        alert("Window closed");
    }
}

Can anyone please help me for the issue with refresh button?

Arun
  • 3,640
  • 7
  • 44
  • 87
  • 1
    possible duplicate of [Detect Browser Refresh in Javascript](http://stackoverflow.com/questions/3247743/detect-browser-refresh-in-javascript) – 0x6C77 Sep 01 '14 at 12:24

3 Answers3

0

I think it will work

$("*").keypress(function(e)
{
if (e.keyCode == 116) {
     e.preventDefault();
  }

}); 
sijo vijayan
  • 1,590
  • 1
  • 21
  • 34
0

Try this:

window.onbeforeunload = function(e){
    //call logout process here
}

onbeforeunload is an event that fires when a window is about to unload its resources. The document is still visible and the event is still cancelable, in other words, whenever you refresh or close a page it fires an event 'onbeforeunload'. When this event returns a non-void value, the user is prompted to confirm the page unload. In most browsers, the return value of the event is displayed in this dialog.

Reference: https://developer.mozilla.org/en-US/docs/WindowEventHandlers.onbeforeunload

Ovais
  • 374
  • 2
  • 7
0
window.onbeforeunload = function(e){
    //call logout process here
}

Any custom declaration on OnBeforeUnload will not be executed as the support is removed from the code. So, the above solution will not work for Chrome. Instead, use HttpContext.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);