Change the behavior of the F5 button

I want to change the behavior of the F5 refresh button of the browser
for example when user click on F5 the page will not be refreshed and some alert will appear ,Currently I’ve tried with this code(which I find in after search in the forum) but it’s not working,Im having reference to jquery cdn.any idea what is missing?

function disableF5(e) {
debugger;
    alert("clicked");
    if (e.which === 116 || e.keyCode === 116) {

        e.preventDefault();
        alert("clicked");
    }
}

$(function() {

    $(document).on("keydown", disableF5);
});

I also tried like following which is not working either ,any idea what I miss here?

  • matchMedia returns null when the FireBug is enabled
  • Space bar keyCode(32) not working on Mozilla Firefox
  • Vimeo iFrame Stealing Mouse Wheel Event on Firefox
  • Is `-moz-transform: scale(2)` equivalent to CSS `zoom: 2`?
  • How can I make event.srcElement work in Firefox and what does it mean?
  • jQuery.height() behaves differently in WebKit and Firefox when using box-sizing:border-box
  •  document.addEventListener('keydown', function(e) {
         debugger;
         if(e.which === 116 || e.keyCode === 116) {
             alert("1234");
             e.preventDefault();
         }
    

    Edit

    also tried with the following and still its not stop in the debugger and I dont see the alert (all the example which I provide stops in the document ready)

    function disableF5(e) {
        if ((e.which || e.keyCode) === 116 || (e.which || e.keyCode) === 82) {
            debugger;
            alert("test")
            e.preventDefault();
        }}
    
        $(document).ready(function() {
            $(document).on("keydown", disableF5);
        });
    

  • Why is the Javascript width of a table cell one pixel too short?
  • syntaxerror json.parse unexpected character at line 1 column 1 of the json data
  • Background image not changing onclick until mouse move in Firefox
  • Firefox addon: new tab extension
  • Unique popup window using window.open in an iframe
  • Event which triggers before DOMContentLoaded
  • 2 Solutions collect form web for “Change the behavior of the F5 button”

    Try using this method that uses JavaScript and Jquery:

    function disableF5(e) {
      if ((e.which || e.keyCode) == 116 || (e.which || e.keyCode) == 82)
      e.preventDefault();
    };
    
    $(document).ready(function(){
      $(document).on("keydown", disableF5);
    });
    

    Note:
    I would not recommend preventing the page from refreshing however, I would recommend using a popup warning users about navigating away from the page eg:

    window.onbeforeunload = function() {
        return "Are you sure you want to leave?";
    }
    

    source
    I hope I helped

    Are you sure that other code can’t interfere?

    I have tried this one on a blank Page:

    <html>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script type="text/javascript">
    function disableF5(e) {
      debugger;
      alert("F5 Hit");
      if (e.which === 116 || e.keyCode === 116) {
        e.preventDefault();
        alert("F5 Identified");
      }
    }
    
    $(function() {
        alert("Page loaded");
        $(document).on("keydown", disableF5);
    });
    </script>
    
    </html>
    

    And it does what it should do

    1. I open the Page
      => Alert “Page loaded”

    2. I hit F5
      => Alert “F5 Hit”
      => Alert “F5 Identified”

    Since I do not get another “Page loaded” alert, I say, the page is not reloaded then I hit F5

    May be you somewhere in your other Javascript override the keydown Handler for the document?

    Do you get your alerts?

    Edit:

    e.keyCode === 82 identifies the “r” key … only makes sense when you use the “ctrl” key, too (ctrl + r = reload)

    if(e.keyCode === 82 && e.ctrlKey)