Well, you could do it (at least in some browsers, I'm not sure if this works in a cross browser), but it will be a pretty bad user interface.
$(document).bind('keypress keydown keyup', function(e) { if(e.which === 116) { console.log('blocked'); return false; } if(e.which === 82 && e.ctrlKey) { console.log('blocked'); return false; } });
In any case, even if it works, there are other ways the user updates the site. By pressing ctrl + r ( cmd + r ) or just click the refresh button. Well, another hotkey combination may be blocked by a similar one, but not block the update button.
-
Perhaps it could be a great idea not to block the default behavior of the browser, but to request gracefully. This can be done by attaching an event handler to the onbeforeunload event, which fires before the site is unloaded.
$(window).bind('beforeunload', function() { return 'Are you really sure?'; });
jAndy
source share