Skip to content


Subversion checkout URL

You can clone with
Download ZIP


improve preventDefault for IE8 #2481

kguelzau opened this Issue · 3 comments

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 referenced this issue from a commit in sarvaje/mootools-core
@sarvaje sarvaje Fix Issue #2481 77dad42

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.