JavaScript: Event.preventDefault Cross Browser

The event.preventDefault() method stops the default actions of an element from happening. For example:

  • Prevent a submit button from submitting a form>
  • Prevent a link from following the URL

event.preventDefault(); can use in most morden browsers including IE 9 above.

However it is not support in IE 6 ~ 9. Instead, event.returnValue = false; is able to achieve the same result in in IE 6 ~ 9.

In order not to get an error thrown, you can test for the existence of preventDefault as below:

Leave a Reply

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

CAPTCHA