5

i am using a timer on my php page but when some refresh the page the timer start from begin. i want to disable f5 or reload option on my php page.

Thanks in advance

Ankit Agrawal
  • 6,034
  • 6
  • 25
  • 49

3 Answers3

9

I test it and it works.

function disable_f5(e)
{
  if ((e.which || e.keyCode) == 116)
  {
      e.preventDefault();
  }
}

$(document).ready(function(){
    $(document).bind("keydown", disable_f5);    
});
Ankit Agrawal
  • 6,034
  • 6
  • 25
  • 49
3

Can't be done – that behaviour is controlled by the end user, and rightly so!

The nearest thing you can do is to display a warning when the window is closed or refreshed, which the user can either decide to heed your warning and stick with the current page, or ignore your request and close/refresh anyway.

See here: Alert when browser window closed accidentally

Community
  • 1
  • 1
JoLoCo
  • 1,355
  • 2
  • 13
  • 23
  • Also, the fact that your page was generated with PHP is irrelevant. PHP (a server-side scripting language) generates text output from the server, usually outputting client-side languages such as HTML and Javascript. It's the output from the server that's important here, not how it's generated. I hope that helps! – JoLoCo Mar 31 '13 at 19:09
  • 1
    so any other option to prevent my time to start with begining when page reload – Ankit Agrawal Mar 31 '13 at 19:12
  • 2
    You could store the timer info in a session cookie so that if the user refreshes the page (or even leaves then comes back) then the timer remains. But that's another question altogether! – JoLoCo Mar 31 '13 at 19:13
2

It is impossible to disable such browser functionality. Would you like it if a page disabled your F5 key? I doubt it. If you are trying to implement some kind of security here, I suggest looking at proper server-side verification approaches.

jwueller
  • 30,582
  • 4
  • 66
  • 70