最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wordpress - Browser: Prevent POST data resubmit on refresh using only JavaScript - Stack Overflow

programmeradmin3浏览0评论

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.

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.

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!

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.

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.

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!

Share Improve this question edited Sep 30, 2020 at 11:07 double-beep 5,50419 gold badges40 silver badges48 bronze badges asked Aug 13, 2017 at 1:53 dtbakerdtbaker 4,9196 gold badges30 silver badges31 bronze badges 3
  • If the save is happening via ajax queries, any number of refreshes won't cause a resubmit. It is very simple - the POST is happening via ajax. You refresh the page, you're only refreshing the content, not the POST. – PressingOnAlways Commented Aug 13, 2017 at 2:00
  • Thanks @PressingOnAlways however the POST is a normal browser submit, not via any fancy ajax/XHR/frame submission. The $_POST data is printed on the page, that wouldn't happen if the submission happened separately via ajax. – dtbaker Commented Aug 13, 2017 at 2:09
  • Same http request/response headers with or without js enabled. There's no XHR stuff in chrome console either. – dtbaker Commented Aug 13, 2017 at 2:12
Add a comment  | 

1 Answer 1

Reset to default 23

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>
发布评论

评论列表(0)

  1. 暂无评论