Cannot override ctrl + s in Firefox using jQuery hotkeys

I am using jQuery Hotkeys plugin: http://code.google.com/p/js-hotkeys/

Here is the code I'm using:

$(document).bind('keydown', 'Ctrl+s', function(event) { alert('saving?'); return false; }); 

It works fine in Chrome, and the default functionality of Ctrl + is disabled, but in Firefox it triggers a warning and also tries to save the html page.

I know there must be something to make it work, Wordpress in Firefox allows you to press ctrl + s to save.

Any ideas?

+4
jquery jquery-plugins jquery-hotkeys
source share
2 answers

Looks like a bug in Firefox, where alert disrupts the synchronization of your code. Delaying the warning seems to get around the problem:

 $(document).bind('keydown', 'Ctrl+s', function(event) { setTimeout(function() { alert('saving?'); }, 0); return false; }); 

Jsbin


Here is a test case confirming my mistake.

 $(document).bind('keydown', 'Ctrl+s', function(event) { event.preventDefault(); }); 

The above ( bin ) will allow you to successfully save the save dialog. Now, if you add a warning before or after it, the save dialog will be , however, if you execute event.preventDefault() and event.stopImmediatePropagation() or return false :

 $(document).bind('keydown', 'Ctrl+s', function(event) { event.preventDefault(); event.stopImmediatePropagation(); alert('saving?'); return false; }); 

Bin

event.preventDefault() alone is enough to prevent the save dialog, if there is no alert s, now with a warning you can prevent the default action.

+8
source share

This worked for me:

 <script> $(document).bind('keypress', 'Ctrl+s', function (event) { event.preventDefault(); alert('saving?'); }); </script> 
+1
source share

All Articles