Detect F5 being pressed and Refresh

倾然丶 夕夏残阳落幕 提交于 2019-12-06 12:58:13

Pressing F5 or physically clicking the browser refresh behaves similarly to navigating away from the page. This is captured in the event window.onunload. Try the snippet example below:

window.onbeforeunload = function (evt) {
  var message = 'Are you sure you want to leave?';
  if (typeof evt == 'undefined') {
    evt = window.event;
  }
  if (evt) {
    evt.returnValue = message;
  }
  return message;
}

This will capture the refresh and allow you to do something or prompt the user.

Reemember that hotkeys are processed in the client side in the browser. The easiest way to implement this is through javascript.

Look at the following link:

http://snippets.dzone.com/posts/show/3552

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