I think...
All You Need
if (event.preventDefault) { //check if browser supports preventDefault
event.preventDefault();
} else { //if no support -> IE lte 8
event.returnValue = false;
}
or shorthand:
event.preventDefault ? event.preventDefault() : event.returnValue = false;
wrap it in a function for faster usage e.g. :
<a href="#something" onclick="prevent();">click prevent</a>
<script>
prevent = function() { // in this case no need to pass 'event'
event.preventDefault ? event.preventDefault() : event.returnValue = false;
console.log('event prevented :)');
}
</script>
Why
all modern browsers support event.preventDefault, but IE supports it from v9
for older IE versions (this is what e.g. jQuery uses) you hook up to the returnValue of an event.
*jQuery Code for reference
jQuery.Event.prototype = {
[...]
preventDefault: function() {
var e = this.originalEvent;
this.isDefaultPrevented = returnTrue;
if ( !e ) {
return;
}
// If preventDefault exists, run it on the original event
if ( e.preventDefault ) {
e.preventDefault();
// Support: IE
// Otherwise set the returnValue property of the original event to false
} else {
e.returnValue = false;
}
},
[...]
I hope i didn't make any mistakes here ;)
Good Luck...