10

Does internet explorer does not support e.preventDefault in javascript? If not what is the reason and what are the alternatives? Please do not suggest any solutions with libraries like jQuery, mootools, etc.

if (!e) e = window.event;

if (e.preventDefault) { 
    e.preventDefault(); 
} else {
    e.returnValue = false;
}
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
sushil bharwani
  • 29,685
  • 30
  • 94
  • 128
  • 2
    possible duplicate of [event.preventDefault() function not working in IE. Any help?](http://stackoverflow.com/questions/1000597/event-preventdefault-function-not-working-in-ie-any-help) (funny, this was the first hit when googling for `event preventdefault in ie`) – Felix Kling Dec 18 '10 at 17:41
  • 1
    @Felix I am more inclined towards why it doesnt works and if its somehow related to IE event model.Moreover the link talks more about mootools and jQuery which i aint intersted in. – sushil bharwani Dec 18 '10 at 17:43
  • 1
    But the accepted answer is not framework specific. – Felix Kling Dec 18 '10 at 17:55
  • @Felix i understand it. I was really wishing to know if its something related to how events are handled in IE Event Model and DOM 2 event Model. Thanks for ur answer – sushil bharwani Dec 18 '10 at 17:58
  • @sushil bharwani: Of course it will be related to how events are handled, but what difference does it make? So MS decided to look for `event.returnValue` instead for some other property. I don't think there is any magic involved. I could just imagine they named it like that because returning `false` in an event handler cancels the event too. – Felix Kling Dec 18 '10 at 18:02
  • @felix ya i agree with u and got ur point. – sushil bharwani Dec 18 '10 at 18:06
  • IE9 supports preventDefault in Standards mode. – EricLaw Dec 20 '10 at 02:17

2 Answers2

14

Ok if you insist:

event.preventDefault does not work, because it does not exist in IE.

For the reason why it does not exist, one would have to work for MS. But in general, IE is not always conform to the standards.

The documentation says you can set event.returnValue:

false
Default action of the event on the source object is canceled.

and further:

Remarks

The value of this property takes precedence over values returned by the function, such as through a Microsoft JScript return statement.

Standards Information

There is no public standard that applies to this property.

preventDefault seems to be supported in IE9.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 1
    i am using IE 7 and event.returnValue = false; also doesnt seems to work.Added my code above see if you can point out something. – sushil bharwani Dec 18 '10 at 18:05
  • 1
    @sushil bharwani: You have to do `if (e.preventDefault)` (without parenthesis). You don't want to call the function, you want to test whether it is defined. – Felix Kling Dec 18 '10 at 18:11
  • Not sure if it's a documentation bug or what, but I'm seeing events without preventDefault in IE9. Perhaps it wasn't in earlier releases? – polm23 Jan 18 '13 at 03:58
-5

It does not support it because someone in Microsoft decided so.

Alternative is the event.cancelBubble.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208