improve preventDefault for IE8 #2481

kguelzau opened this Issue Mar 20, 2013 · 3 comments


None yet

2 participants


Here is a small improvement (aka hack) to use CTRL + O in IE8 and prevent the default dialog of "File open":

preventDefault: function(){
  if (this.event.preventDefault) this.event.preventDefault();
  else this.event.returnValue = false;
  // additionally reset keyCode
  try { this.event.keyCode = 0; } catch (e) {}
  return this;

To also use Ctrl + O/P in IE9/10 you have to use "attachEvent" enstead of "addEventListener".


So for now I have to patch the mootools code (Core or More.Keyboard) to be able to use those Shortcuts in IE 9/10 :-\

[Element, Window, Document].invoke('implement', {
  addListener: function(type, fn){
    // inverted calls for keydown
    if (type == 'keydown') {
      if (this.attachEvent) this.attachEvent('on' + type, fn);
      else this.addEventListener(type, fn, !!arguments[2]);
    } else {
      if (this.addEventListener) this.addEventListener(type, fn, !!arguments[2]);
      else this.attachEvent('on' + type, fn);
  return this;
@sarvaje sarvaje added a commit to sarvaje/mootools-core that referenced this issue May 22, 2013
@sarvaje sarvaje Fix Issue #2481 77dad42
MooTools member

I don't think we want to fix this in MooTools. Especially because it's not really a common issue, not clearly specified in any specification and with different implementations (for example Chrome doesn't prevent ctrl+w while it does prevent other combinations).

I don't particularly like preferring attachEvent to the standard addEventListener.

Perhaps it's a cleaner solution to have a separate module/function that you can can call once to fix this. e.g.:


function preventDefaultKeys(element){
    if (element.attachEvent) element.attachEvent('keydown', function(e){
        (e || window.event).keyCode = 0;

It's ok to not fix this special ie stuff in core.
For me it was easier to patch core instead of create additional code everywhere i use shortcuts.

Perhaps this issue/thread will be useful for someone searching for ;-)

@kguelzau kguelzau closed this May 23, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment