Sometimes, we want to disable F5 and browser refresh using JavaScript.
In this article, we’ll look at how to disable F5 and browser refresh using JavaScript.
How to disable F5 and browser refresh using JavaScript?
To disable F5 and browser refresh using JavaScript, we stop the default behavior when pressing F5.
For instance, we write
document.addEventListener("keydown", (e) => {
if (e.keyCode === 116) {
e.preventDefault();
}
});
to call addEventListener to listen to the keydown event.
In it, we check if the keyCode property is 116, which is the code for F5.
If it’s true, then we call preventDefault to stop the default refresh behavior.
Conclusion
To disable F5 and browser refresh using JavaScript, we stop the default behavior when pressing F5.