Ad Code

Disable Refresh in Webpage Using Javascript (F5 & Ctrl + R)

document.onkeydown = function() {   
    switch (event.keyCode) {
        case 116 : //F5 button
            event.returnValue = false;
            event.keyCode = 0;
            return false;
        case 82 : //R button
            if (event.ctrlKey) {
                event.returnValue = false;
                event.keyCode = 0; 
                return false;
            }
    }
}

Same as before, you can add this to your main javascript file and place it in the head part of your page.

It works by adding a function to your page's onkeydown event. Then we simply get which button was pressed by using event.Keycode (116 for F5 and 82 for R). Also, notice the "if" statement for R, it simply checks if the control button was pressed at the same time as R by checking event.ctrlKey.

But there's a limitation to handling page refreshes with this code. You simply cannot control the refresh button specific to your browsers' toolbar.

Post a Comment

0 Comments