I have been trying to understand why sometimes IE8 doesn't like PreventDefault and why sometimes it seems to be OK (no errors). From what I have read, including here at SO is that events in jquery are normalised so preventDefault will always exist with a jQuery event. However regular javascript event bindings is when the following workaround is needed for ie8:
event.preventDefault ? event.preventDefault() : event.returnValue = false
Is this correct?
So if you are using jQuery .click .bind .on etc to handle an event then PreventDefault will not cause errors in IE8, such as:
$('a').on('click', function(e) {
e.preventDefault(); //no need for e.preventDefault ? e.preventDefault() : e.returnValue = false as jquery event?
//code
});
However if it is a regular Javascript event, such onclick then the workaround is needed?
Hope that makes sense.
Thanks
Yes, your understanding sounds correct. Also, if you're using a "DOM0" event handler (eg someElement.onclick = function(e) { ... }
), there is a simpler way to prevent the browser default behaviour that works in all browsers that support events: return false
.
var someElement = document.getElementById("someElementId");
someElement.onclick = function(e) {
// Do some stuff
return false;
};
However, in this case, the event is not passed to the event handler in IE <= 8 and you have to get it from window.event
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.