Prevent User to reload page using jquery or javascript [duplicate]

和自甴很熟 提交于 2019-12-04 08:14:55

No, you can't. If a user wants to reload a page you cannot stop them.

It is not possible to stop the user from page reload/page close if the user wants it. Two methods which you can use are as follows:

You can ask for a confirmation like this:

<script>
window.onbeforeunload = function (e) {
e = e || window.event;

// For IE and Firefox prior to version 4
if (e) {
    e.returnValue = 'Sure?';
}

// For Safari
return 'Sure?';
};
</script>

Here is the fiddle for above: http://jsfiddle.net/gopi1410/NujmL/

You can preventDefault on pressing of F5 key using:

document.onkeydown = function(event) {
  if(window.event){
        event.preventDefault();
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!