Skip to content Skip to sidebar Skip to footer

How To Detect An F5 Refresh Keypress Event In Jquery?

I noticed that jquery has a keypress function. But it seems that it can only detect keypress event of numbers and characters. It cannot detect the F5 keypress event. And What surpr

Solution 1:

I don't know what you did wrong in your code, but jQuery does say F5 is 116 and t is 84:

http://jsfiddle.net/Brjb2/1

One possible error is keypress will have different keycode, that's why keydown is more preferred.

        |  T  |  A  |  F5keydown |  86 |  65 | 116keypress| 116 |  97 |  -

Also pressing F5 will not trigger keypress because the reload part happens before keypress.

Solution 2:

The keypress event does not accept function keys(F1-F12). You can try to use keydown event.

Solution 3:

this code works good for me i tested it in chrome and firefox successfully

<script>document.onkeydown = capturekey;
    document.onkeypress = capturekey;
    document.onkeyup = capturekey;

    functioncapturekey(e) {
        e = e || window.event;
        //debuggerif (e.code == 'F5') {
            if (confirm('do u wanna to refresh??')) {
                //allow to refresh
            } 
            else {
                //avoid from refresh
                e.preventDefault()
                e.stopPropagation()
            }
        }
    }
</script>

Post a Comment for "How To Detect An F5 Refresh Keypress Event In Jquery?"