Home >Web Front-end >JS Tutorial >How to Prevent Form Submission in Internet Explorer?
Preventing Form Submission in IE: Overcoming the event.preventDefault() Limitation
As an extension of the event.preventDefault() function, this article delves into the issue of its lack of support in Internet Explorer.
The original code provided demonstrates a form validation process using JavaScript's Mootools library. In browsers other than IE, the event.preventDefault() method effectively prevents the form from submitting prematurely. However, in IE, an error arises due to the absence of this method.
To resolve this issue, IE offers an alternative approach:
event.returnValue = false;
This line can be seamlessly implemented to achieve the same result as event.preventDefault().
To enhance error handling, a quick check can be performed to determine the availability of event.preventDefault():
if(event.preventDefault) event.preventDefault();
By combining both approaches, the following code ensures compatibility across all major browsers:
event.preventDefault ? event.preventDefault() : (event.returnValue = false);
The above is the detailed content of How to Prevent Form Submission in Internet Explorer?. For more information, please follow other related articles on the PHP Chinese website!