improve preventDefault for IE8 #2481

Closed
kguelzau opened this Issue Mar 20, 2013 · 3 comments

Projects

None yet

2 participants

@kguelzau

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;
}
@kguelzau

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

See: http://stackoverflow.com/questions/13443742/preventing-default-keystroke-actions-in-internet-explorer

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
@arian
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.:

preventDefaultKeys(element);

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

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 https://www.google.de/search?q=mootools+ctrl%2Bo+ie ;-)

@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