Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

hotkeys

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?

like image 963
zjm1126 Avatar asked Aug 26 '10 01:08

zjm1126


1 Answers

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.

like image 185
James Kolpack Avatar answered Sep 29 '22 01:09

James Kolpack