javascript 在我的 php 页面上禁用重新加载和 f5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15733302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:53:15  来源:igfitidea点击:

Disable reload and f5 on my php page

phpjavascriptjquerytimer

提问by Ankit Agrawal

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.

我在我的 php 页面上使用了一个计时器,但是当有人刷新页面时,计时器从头开始。我想在我的 php 页面上禁用 f5 或重新加载选项。

Thanks in advance

提前致谢

回答by Ankit Agrawal

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);    
});

回答by JoLoCo

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

请参阅此处:浏览器窗口意外关闭时发出警报

回答by jwueller

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.

不可能禁用此类浏览器功能。如果某个页面禁用了您的 F5 键,您会喜欢吗?我对此表示怀疑。如果您想在这里实现某种安全性,我建议您查看适当的服务器端验证方法。