Introduction
In this blog, I will share a trick to prevent the user refreshing the Browser, using Keyboard F5 function key by disabling F5 function key, using JavaScript or jQuery.
The idea is to determine the key code of the pressed key and if it is 116 i.e. ASCII key code of the keyboard F5 function key, stop its propagation by returning false inside the event handler.
Using BODY Tag
The very first method is the easiest one to implement. You need to define the onkeydown event handler within the body tag and simply return false, if the pressed key is F5 function key i.e. ASCII key code 116.
- <body onkeydown="return (event.keyCode != 116)">
- <h1>
- Click this page to set focus.
- </h1>
- </body>
- <body>
- <script type = "text/javascript">
- window.onload = function () {
- document.onkeydown = function (e) {
- return (e.which || e.keyCode) != 116;
- };
- }
- </script>
- <h1>
- Click this page to set focus.
- </h1>
- </body>
- <body>
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
- <script type="text/javascript">
- $(function () {
- $(document).keydown(function (e) {
- return (e.which || e.keyCode) != 116;
- });
- });
- </script>
- </body>
Giridhar AddagallaPosted Dec 21, 2017, 9:24 AM
Can we give alert while pressing f5
Humayun Kabir MamunPosted Oct 13, 2016, 4:26 AM
Nice...
SubashPosted Oct 11, 2016, 7:28 AM
Good start