Event.Preventdefault() Function Not Working in Ie

event.preventDefault() function not working in IE

in IE, you can use

event.returnValue = false;

to achieve the same result.

And in order not to get an error, you can test for the existence of preventDefault:

if(event.preventDefault) event.preventDefault();

You can combine the two with:

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

event.preventDefault() is not working in IE 11 for custom events

I had the same problem and could solve it with the following hack:

var event = document.createEvent('CustomEvent');
event.initCustomEvent('custom', true, true, {});
event.preventDefault = function () {
Object.defineProperty(this, "defaultPrevented", {get: function () {return true;}});
};
event.preventDefault();
event.defaultPrevented; // true

evt.preventDefault is not working in IE and Edge on mouse move event, even tried evt.returnValue = false; but didn't work to stop propagation

Instead of making evt.preventDefault(); in mouse move make it in mousedown/Click event itself

private MouseDown(evt: any) {
this.viewState.resizing = true;
const {ownerDocument} = ReactDOM.findDOMNode(this);
evt.preventDefault();
evt.stopPropagation();
ownerDocument.addEventListener('mousemove', this.MouseMove);
ownerDocument.addEventListener('mouseup', this.MouseUp);

this.setState(this.viewState);
}

event.preventDefault in IE 10

Just do the below, jQuery has already done the cross browser job for you.

$("#fileform").submit(function(e){
e.preventDefault();
// other code
});

event.preventDefault() or return false don't work in IE9

In IE9 the legacy event handler model is still partial used. preventDefault() works only, when the event listener is attached using addEventListener().

If you want to prevent default action from an inline handler, you have to use the legacy method:

event.returnValue = false;
event.cancelBubble = true; // This is affects like event.stopPropagation() in older IEs

Though jQuery not working is odd, I've no explanation for that... Unless you're running IE in compatible mode and use jQuery 2.X?


EDIT

Also a reference to console object will break the code in IE<10, if Dev Tools are not opened. You can find a lot of fixes for this problem at SO. My favorite is this:

// The very first lines in the global context
if (!window.console) {
window.console = {
log: function () {}
// Add other console methods, if the scripts on the page are using them
}
}

Though the console problem can be avoided with the code above, it's always better to remove all loggings from the final code to be published.

Why Internet Explorer stop after a function when use event.preventDefault() or return false

Can someone explain to me why this happens?

It's a bug in Internet Explorer (what else). The change event is supposed to be not cancelable, in contrast to click events (see Is event.preventDefault cancelling change events?, jQuery/Javascript: Click event on a checkbox and the 'checked' attribute and Why does preventDefault() on a parent element's click 'disable' a checkbox?). IE8 however does prevent the checkbox from being (un)checked when the change event is canceled. Try it here.

How to work around that? Just remove e.preventDefault() (or return false) from your code. I don't see any reason to use it anyway.



Related Topics



Leave a reply



Submit