Disabling the browser's 'ctrl+s' default function

zjm1126 picture zjm1126 · Aug 26, 2010 · Viewed 10.4k times · Source

I use jQuery-hotkeys

And the following code:

$(document).bind('keydown', 'ctrl+s', function(){$('#save').click()});

but I cannot disable the browser's default behavior. How do I disable it?

Answer

James Kolpack picture James Kolpack · Aug 26, 2010

It looks like you return false from your handler to disable "bubbling up" the event. So:

$(document).bind('keydown', 'ctrl+s', function(){$('#save').click(); return false;});

... but it may be browser specific. From your link:

Firefox is the most liberal one in the manner of letting you capture all short-cuts even those that are built-in in the browser such as Ctrl-t for new tab, or Ctrl-a for selecting all text. You can always bubble them up to the browser by returning true in your handler.

Others, (IE) either let you handle built-in short-cuts, but will add their functionality after your code has executed. Or (Opera/Safari) will not pass those events to the DOM at all.

So, if you bind Ctrl-Q or Alt-F4 and your Safari/Opera window is closed don't be surprised.