0

I am able to detect Ctrl+R but unable to stop reloading page.

Please help me to fix this.

I am using this code.

$(document).keydown(function(e) {
    if (e.keyCode == 65+17 && e.ctrlKey) {
        alert('ctrl R');
        exit;
        return ;
    }
});

Thanks in advance.

Believe It or Not
  • 631
  • 2
  • 8
  • 21

2 Answers2

0
    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script>
     $(function () {
document.onkeydown = KeyPress;
function KeyPress(e) {
console.log(e);
      var press = window.event? event : e
      if (press.keyCode == 82 && press.ctrlKey) alert("Ctrl+R");  

     if ($.browser.mozilla) {
                if (e.ctrlKey && keycode == 82) {
                    if (e.preventDefault)
                    {
                        e.preventDefault();
                        e.stopPropagation();
                    }
                }
            } 


if ($.browser.msie) {
            if (window.event.ctrlKey && press.keycode == 82) {
                window.event.returnValue = false;
                window.event.keyCode = 0;
                window.status = "Refresh is disabled";
            } 
}
}

});
    </script>
    </head>
    <body>

    <p>If you click on me, I will disappear.</p>
    <p>Click me away!</p>
    <p>Click me too!</p>

    </body>
    </html>
HudsonPH
  • 1,838
  • 2
  • 22
  • 38
0

The standard / clean way to help user prevent unwanted page reload is via beforeunload and not via overriding key event, which is, in fact, futile: you do not know what key combination invoked page reload (for instance, f5 works alike in most browsers), he may press CTRL+R with locationbar focused so your page gets no event to capture, he may have pressed toolbar button…

Mentioned standard approach from linked MDN page

window.addEventListener("beforeunload", function (e) {
  var confirmationMessage = "\o/";

  e.returnValue = confirmationMessage;     // Gecko, Trident, Chrome 34+
  return confirmationMessage;              // Gecko, WebKit, Chrome <34
});

This will prompt user whenever he tries to reload / close / navigate away from your page no matter what initiated unload.

myf
  • 9,874
  • 2
  • 37
  • 49