Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preventDefault for save popup is working in chrome but not in firefox

Hi i like to prevent the default behaviour of ctrl+s(save) so i tried the below code

jQuery(document).bind("keydown", function(e) {
  if(e.ctrlKey && (e.which == 83)) {
    e.preventDefault();
    alert("Ctrl+S");
    return false;
  }
}); 

But this is preventing only in chrome not in firefox.In firefox the save dialog is poping up why is this happening or am i missing something.Any help?

like image 853
sun Avatar asked Dec 04 '25 05:12

sun


1 Answers

Try this....

$(document).bind("keydown", function(e) {
  if(e.ctrlKey && (e.which == 83)) {
    e.preventDefault();
    setTimeout(function() {
      alert("Ctrl+S");
    }, 1000);
    return false;
  }
});

Using an alert will complete the thread execution, only for the thread to be picked up again once the OK button of the alert is clicked. I can only imagine that at this point the 'preventDefault' just hasn't been acknowledged by Firefox (even though you're calling this before the alert) and it continues to display the Save As dialog.

I've used a setTimeout to delay the alert by 1 second, although I'm sure using 0 will have the same effect... simply pushing the function (now async) showing the alert to the back of the execution stack, which will allow the browser to complete the execution of the preventDefault.

I know you don't want the 'alert', but thought I'd provide an explanation of the behaviour.

like image 173
Mark Graham Avatar answered Dec 06 '25 17:12

Mark Graham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!