最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change the behavior of the F5 button - Stack Overflow

programmeradmin4浏览0评论

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?

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

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?

 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);
    });
Share Improve this question edited Oct 20, 2014 at 16:00 LiamWilson94 4583 gold badges8 silver badges26 bronze badges asked Oct 20, 2014 at 15:19 John JerrbyJohn Jerrby 1,7037 gold badges34 silver badges69 bronze badges 2
  • Is it even possible? I think some special keys cannot be changed, like echap. But I could be wrong. – Vadorequest Commented Oct 20, 2014 at 15:31
  • 1 @Vadorequest- take a look at the following post stackoverflow./questions/2482059/… – John Jerrby Commented Oct 20, 2014 at 16:03
Add a ment  | 

2 Answers 2

Reset to default 8

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 remend preventing the page from refreshing however, I would remend 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./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)
发布评论

评论列表(0)

  1. 暂无评论