Categories
Coding

Prevent Form Resubmission on Page Refresh

We face a common issue on many websites that whenever a form submits to the same page, and then we refresh the page, the form data is submitted again. Sometimes the browser asks for confirmation, sometimes it does not. It becomes really frustrating if it causes an operation to happen twice.

To prevent this on our website, all we need to do is add a small Javascript code snippet, and the form resubmission issue will be gone.

Here is the code-

<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>

By Abhineet Mittal

Abhineet is IIM Alumnus and Founder of popular Digital Publishing Platform, Sahityapedia. He is a technology enthusiast by hobby & an avid reader.

Leave a Reply

Your email address will not be published.