Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser: Prevent POST data resubmit on refresh using only JavaScript

I am aware of the many Post/Redirect/Get questions on here. This one is a bit different. I'm unable to find an answer that explains this particular JavaScript solution.

When you submit a form via POST and then hit the refresh button afterwards, the browser will prompt me to re-submit data. HOWEVER this prompt does not happen in the WordPress backend (when JS is enabled) and there is no Redirect/Get after the Post.

I've tried to show this in a series of screenshots below. It shows the first POST submit with the POST data printed on the page, and then the refresh causes a GET without any browser re-submit prompt.

browser resubmit without post

When I disable JavaScript and hit refresh, I get the expected "Would you like to resubmit your data?" prompt and the refresh causes a second POST.

js disabled, normal post resubmit happens

So wordpress is doing some JavaScript magic here to prevent POST data resubmission on refresh/back button.

Can anyone point me to the code in WordPress that shows how they achieve this using only JavaScript? I have no idea where to even start searching.

Do they do something with the pushstate?

Thanks!

like image 586
dtbaker Avatar asked Aug 13 '17 01:08

dtbaker


People also ask

How do I stop confirmation resubmission on refresh?

You can prevent form resubmission via a session variable. Yes we can use microtime() as well as time() also instead of rand() , whatever function or variable that gives different value we can use it. BUT make sure that you set that value to SESSION variable.

How do I stop resubmission on page refresh in HTML?

One way to stop page resubmission on page refresh is to unset the form data after it is submitted so that the variable storing form data becomes empty and wrap up your form processing block of codes to check if the form is empty.

How do I turn off confirmation resubmission in chrome?

Solution 1: Disable Confirm Form Resubmission From Chrome Follow these steps below to disable the confirm resubmission feature from chrome, if you're windows user; Right click on your chorme shortcut, select properties. In the target field, add: “-disable-prompt-on-repost” without the quotes after chrome.exe.

How do I stop resubmit from refreshing in asp net?

First thing you can do is you need to add required field validations to the textboxs. Then you have to empty the texboxes in the button_click event after sqlcommand. You can empty the texboxes using TextBox1. Text = String.


1 Answers

Solution: WordPress uses window.history.replaceState on every page load.

This prevents the POST from running again on refresh or back button.

Nifty!

Non-WordPress proof of concept here: https://dtbaker.net/files/prevent-post-resubmit.php

Code is:

<script>
    if ( window.history.replaceState ) {
        window.history.replaceState( null, null, window.location.href );
    }
</script>
like image 150
dtbaker Avatar answered Oct 21 '22 02:10

dtbaker