Sometimes, we want to disable Ctrl + F of find in page with JavaScript.
In this article, we’ll look at how to disable Ctrl + F of find in page with JavaScript.
How to disable Ctrl + F of find in page with JavaScript?
To disable Ctrl + F of find in page with JavaScript, we can attach a keydown event listener.
And then we can listen for key with key code 114 or press of the control key and key with key code 70 and prevent the default action in either case.
To do this, we write:
window.addEventListener("keydown", (e) => {
if (e.keyCode === 114 || (e.ctrlKey && e.keyCode === 70)) {
e.preventDefault();
}
})
to attach a keydown event listener with window.addEventListener
.
Then we check for control key press with e.ctrlKey
and check for regular key press with e.keyCode
.
e.preventDefault
prevents the search box from showing.
Conclusion
To disable Ctrl + F of find in page with JavaScript, we can attach a keydown event listener.
And then we can listen for key with key code 114 or press of the control key and key with key code 70 and prevent the default action in either case.