0

I'm Trying to disable Ctrl++ / Ctrl+- browsers shortcuts via javascript :

$(document).ready(function(){
    $(document).keydown(function(event) {
        if (event.ctrlKey==true && (event.which == '107' || event.which == '109')) {
            alert('disabling zooming ! ');
            event.preventDefault();
         }
    });
});

This code is working great in FF and Chrome , and dosent prevent zooming in IE ! any idea ?

udondan
  • 57,263
  • 20
  • 190
  • 175
Sami Jmii
  • 460
  • 7
  • 17
  • 9
    ["This is an accessibility issue and you should try to work with it and not against it."](http://stackoverflow.com/questions/542576/disable-the-ctrl-wheel-zoom-effect-at-runtime) – jbabey Jan 28 '13 at 16:12
  • 1
    Why are you trying to disable the browser zoom? – Fabrizio Calderan Jan 28 '13 at 16:13
  • I suspect the issue is with `event.preventDefault()`. You can try these solutions: http://stackoverflow.com/q/1000597/1987993 . But I have to agree with jbabey, changing the expected behavior of the browser isn't a great idea, especially when it comes to accessibility features. – Warren R. Jan 28 '13 at 16:15
  • So many ways to trigger this zoom, including Ctrl+wheel, pinching gestures and menu items. You cannot prevent them all in all browsers on all platforms. – deceze Jan 28 '13 at 16:18
  • I Agree with jbabey too , but is there an answer to the question ?! – Sami Jmii Jan 28 '13 at 16:21
  • Did you try the solution in the link I provided? – Warren R. Jan 28 '13 at 16:44
  • Also, you may need to use event.keyCode instead of event.which for older versions of IE. I am not making an official answer because I don't have a computer with IE in front of me so I can't test it for you. – Warren R. Jan 28 '13 at 16:49
  • Warren R : I added event.returnValue = false; inside the if and that doesn't work in IE – Sami Jmii Jan 28 '13 at 17:01

2 Answers2

0

This works for me, though you may want to bind to 'keyup' also.

$(document).ready(function () {
        $(document).bind('keydown keypress', function (event) {
            event.preventDefault();
        });
    });
JakeSidSmith
  • 819
  • 6
  • 12
  • This blocks ALL keyboard interaction with the page. That includes halting page load (ESC), scrolling (up, down, page up, page down, etc.) – Igor Zinken Sep 23 '15 at 11:54
0

There are more than 2 button numbers you have to preventDefault in order to disable fully the scrolling. Me personally I disable all ctrl key combinations.

$(document).ready(function () {
    function preventDefault(e) {
          e = e || window.event;
          if (e.preventDefault)
              e.preventDefault();
          e.returnValue = false;
        }
    $(document).bind('keydown keypress', function (event) {
        if (event.ctrlKey) {
            preventDefault(event);
            return false;
        }
    });
});