Internet Explorer 8 and event.preventDefault() Problem


I was trying to use event.preventDefault in a function called by an onclick method of an anchor tag. Although this worked fine in Firefox and chrome, naturally, I.E. would find it necessary to treat this differently. IE 8 sadly does not support event.preventDefault() (I.E. 9 however, does add support for preventDefault). However, this can easily be gotten around by using a simple test:

(event.preventDefault) ? event.preventDefault() : event.returnValue = false;

This will check if preventDefault is defined, and if not, it will use IE’s event.returnValue instead. As far as I can tell, event.returnValue=false will achieve the same goal in most cases.

This entry was posted in Html, JavaScript and tagged , , , , , . Bookmark the permalink.

4 Responses to Internet Explorer 8 and event.preventDefault() Problem

  1. RajkumaR says:

    Super it work thank you bosssssssssssssss………………..

  2. Alan says:

    Thank you thank you THANK YOU! Apparently it’s not enough to just set event.returnValue, and your approach just saved me a lot of time! Thanks again.

  3. Suvasish says:

    It saved my time too :). Thank you

  4. Hamid says:

    It saved my time and cleanded my code.
    thnak’s a lot :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>