Disable F5 and browser updates using JavaScript

I want to disable browser refresh using javascript.

I am currently using window.onbeforeunload and I do not want it to be called when the user updates the browser.

What is the best way to do this?

+46
javascript javascript-events
Mar 20
source share
10 answers

Update. A recent comment claims that this does not work in the new Chrome ... As shown in jsFiddle and tested on my personal site, this method still works with Chrome ver 26.0.1410.64 m

In jQuery, this is REALLY REALLY:

jsFiddle

 // slight update to account for browsers not supporting e.which function disableF5(e) { if ((e.which || e.keyCode) == 116) e.preventDefault(); }; // To disable f5 /* jQuery < 1.7 */ $(document).bind("keydown", disableF5); /* OR jQuery >= 1.7 */ $(document).on("keydown", disableF5); // To re-enable f5 /* jQuery < 1.7 */ $(document).unbind("keydown", disableF5); /* OR jQuery >= 1.7 */ $(document).off("keydown", disableF5); 

On the side of the note: This disables only the f5 button on the keyboard. To truly disable the update, you must use the server side of the script to check for page state changes. I can’t say that I really know how to do this, since I have not done it yet.

On the software site I work for, we use my disableF5 function in conjunction with Codeigniter session data. For example, there is a lock button that locks the screen and opens a dialog with a password. The disableF5 function is quick and easy, and this button does nothing. However, to prevent a mouse click on the refresh button, a couple of things happen.

  • When you click on a lock, user session data has a variable called "locked" that becomes TRUE
  • When you click the refresh button on the loading method of the main page, a check is performed against the session data for β€œblocked”, if TRUE, then we simply do not allow redirection and the page never changes, regardless of the requested destination



TIP: Try using a cookie on the server, such as PHP $_SESSION or even .Net Response.Cookies , to support "where" your client is on your site. This is more of a vanilla way to do what I do with the CI session class. The big difference is that CI uses a table in your database, while these vanilla methods store an editable cookie in the client. However, the disadvantage is that the user can clear his cookies.

+65
Nov 03 2018-11-11T00:
source share

From Enrique site sent:

 window.history.forward(1); document.attachEvent("onkeydown", my_onkeydown_handler); function my_onkeydown_handler() { switch (event.keyCode) { case 116 : // 'F5' event.returnValue = false; event.keyCode = 0; window.status = "We have disabled F5"; break; } } 
+8
Mar 20 '10 at 7:03
source share

for mac cmd + r, cmd + shift + r.

 function disableF5(e) { if ((e.which || e.keyCode) == 116 || (e.which || e.keyCode) == 82) e.preventDefault(); }; $(document).ready(function(){ $(document).on("keydown", disableF5); }); 
+8
Jul 08 '13 at 19:11
source share
 var ctrlKeyDown = false; $(document).ready(function(){ $(document).on("keydown", keydown); $(document).on("keyup", keyup); }); function keydown(e) { if ((e.which || e.keyCode) == 116 || ((e.which || e.keyCode) == 82 && ctrlKeyDown)) { // Pressing F5 or Ctrl+R e.preventDefault(); } else if ((e.which || e.keyCode) == 17) { // Pressing only Ctrl ctrlKeyDown = true; } }; function keyup(e){ // Key up Ctrl if ((e.which || e.keyCode) == 17) ctrlKeyDown = false; }; 
+6
Aug 27 '14 at 8:26
source share
 $(window).bind('beforeunload', function(e) { return "Unloading this page may lose data. What do you want to do..." e.preventDefault(); }); 
+4
Aug 16 '13 at 7:10
source share

This is the code I use to disable IE and firefox updates (this works well for F5, Ctr + F5 and Ctrl + R)

 <script language="javascript" type="text/javascript"> //this code handles the F5/Ctrl+F5/Ctrl+R document.onkeydown = checkKeycode function checkKeycode(e) { var keycode; if (window.event) keycode = window.event.keyCode; else if (e) keycode = e.which; // Mozilla firefox if ($.browser.mozilla) { if (keycode == 116 ||(e.ctrlKey && keycode == 82)) { if (e.preventDefault) { e.preventDefault(); e.stopPropagation(); } } } // IE else if ($.browser.msie) { if (keycode == 116 || (window.event.ctrlKey && keycode == 82)) { window.event.returnValue = false; window.event.keyCode = 0; window.status = "Refresh is disabled"; } } } </script> 

If you do not want to use useragent to determine what type of browser it is ($ .browser uses navigator.userAgent to determine the platform), you can use

if('MozBoxSizing' in document.documentElement.style) - returns true for firefox

+3
Nov 14
source share

It works for me in all browsers:

 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; } } } 
0
Sep 25 '13 at 11:10
source share

Use this for modern browsers:

 function my_onkeydown_handler() { switch (event.keyCode) { case 116 : // 'F5' event.preventDefault(); event.keyCode = 0; window.status = "F5 disabled"; break; } } document.addEventListener("keydown", my_onkeydown_handler); 
0
Dec 22 '13 at 13:52
source share

You can directly use the hotkey with affluent persons if you use JSF.

 <rich:hotKey key="backspace" onkeydown="if (event.keyCode == 8) return false;" handler="return false;" disableInInput="true" /> <rich:hotKey key="f5" onkeydown="if (event.keyCode == 116) return false;" handler="return false;" disableInInput="true" /> <rich:hotKey key="ctrl+R" onkeydown="if (event.keyCode == 123) return false;" handler="return false;" disableInInput="true" /> <rich:hotKey key="ctrl+f5" onkeydown="if (event.keyCode == 154) return false;" handler="return false;" disableInInput="true" /> 
0
Mar 12 '15 at 5:12
source share

If you want to disable ctrl + f5, ctrl + R, f5, backspace, you can use this simple code. This code works in Mozila as well as in Chrome. Add this code to your body tag:

 <body onkeydown="return (event.keyCode == 154)"> 
0
Apr 24 '15 at 12:24
source share



All Articles