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!
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>
Related
This annoying message pops up every time you try to refresh the page that you have already added the product on that page to the cart and also when you fail to save changes in user account details and try to refresh the page.
I've already read the solutions about this problem in other similar questions like:
Change your request type from POST to GET
Change the request type for reloading the page from window.location.reload(); to window.location=window.location;
Redirect users to another page
which seems like such changes should be made directly on Firefox (which is not what I want) except for redirecting users to the shopping cart (which I'm not thinking at this point unless there is no any other solutions)
I want to fix the problem for every WooCommerce user who browses the store using Firefox. so is there any way (other than redirecting) to prevent WooCommerce from triggering this warning message in Firefox?
I am working on a php project in codeigniter. When a user logs out, he/she is directed to login page. But when he/she clicks the back button of browser, he/she gets to see the home page just before the log out, which definitely not a good thing.
I have added session check on every page so that when a user reloads any page after logout he/she will be directed to login page.
As far as i think, this happens due to web browser using cache for the back button.So one solution as given in many stack overflow forums and others is to clear the cache, which i too did. But another problem came that the document expires after back button, which is not good from the UX point of view.
Another solution is to add javascript code to restrict the page to itself when pressing back button, which surely works. But since the javascript is client side, this won't work if you disable it from browser.
If you check the Gmail or facebook or any good site ,they handle it really well and no ,it's not by just javascript code , as suggested in this forum How Google deals with the Back Button after logout? ,
So my question is how do they handle it in real?
On home page, you'll need to check if SESSION is active.
When user is logging out, you'll need to destroy SESSION.
For example:
-On Home Page
session_start();
if(!isset($_SESSION['variableName']))
{
header("location:login.php");
}
On logout page:
session_start();
session_destroy();
Hope this helps.
One time I got that problem and finally I got many tiny solutions for that.
Put noscript tag in common header file. I return a text when the JS turn off.
<noscript>Your browser does not support JavaScript!</noscript>
Another Method to check the user logged or not at every intervel
setInterval(function(){
$("#autorefresh").load("checklogin.php?screenName=autorefresh");
}, 5000);
And finally put this back prevent js
history.pushState(null, null, 'loginController.php?time=
$currentDateTime; ?>');
window.addEventListener('popstate', function(event) {
history.pushState(null, null, 'loginController.php?time=<?php echo $currentDateTime; ?>');
});
This is my experience knowledge... Thank You
I have a form, with a code to show a popup when I press a create/edit link. Now when I do a page refresh, I get the following popup
I have managed to stop the popup from appearing when Retry is pressed, by handling it on the code behind of my aspx, but when Cancel is pressed, the page blinks (I guess it renders again?) and the popup is shown.
It doesn't go back to the server. It just goes to the javascript function that displays the popup, and shows it.
It should be noted at this point that this popup is just a <div> which can be shown or hidden.The default property of this <div> is hidden.
Please help me solve this issue and also explain why this is happening. I haven't been able to find anything on the internet explaining this issue.
When submitting a form, content may be sent with either POST or GET.
Sending with GET appends values to the address defining what webpage you are on. It could look like this:
www.domain.tld/page?value1=apple&value2=banana
Sending with POST sends the value in a hidden field that the server receives.
Clicking "Retry" will load the website with the information currently held within the POST field. Clicking cancel should display the address you are heading to without the POST content.
I hope this answers your question. If not, is there any way for you to show the piece of code that handles the POST data?
The browser saves the data in the form when you submit it, and when you refresh the page, the browser attempts to send this data again. The popup is a warning from the browser that this is about to happen, which is important since the form could be on a shopping site, so resending the data would result in accidentally buying the same things multiple times.
To fix this, you can redirect to another page once the form has been submitted, or you can add code to reset the form so the data won't be sent again.
We should follow a best practice to solve this problem. Better have a look at this. When you press the cancel button, it simply load the previous page and values will be persisted.
My understanding so far is that when you press the cancel button, the values for the page is taken from the browser's cache. I cleared the cache to test this theory. The cache isn't just storing the values of the page but also the last server response received. In my case, the last server response was to show the the popup by calling my javascript function, along with the required values, which is what it did.
Now my work around to it was to make the closing button as a server command as well, so that the final response would be to hide the popup.
Please do let me know if there is something wrong in this explanation.
In my ASP.NET WebForms page I have a Modal window that pops up. The javascript code for displaying this modal window is as follows:
function OpenMailAddressWin(subscriberContactRelationGid, routeId, btn) {
window.showModalDialog("SubscriberSecondaryAddress.aspx" + BuildQueryStringValuesForSubscriber(subscriberContactRelationGid, routeId, returntxtReceiptDate().value), this, strWindowFeatures + ";scroll:no;dialogWidth:442px;dialogHeight:350px");
location.reload(true);
}
After the modal window is closed I need to refresh the parent page (hence the location.reload(true); statement at the end) in order for alterations made in the modal window to take affect.
Now the thing is that sometimes (not every time, infuriatingly) when I close this modal window I get a warning popup which says:
" To display the webpage again, Internet Explorer needs to resend the information you've recently submitted.
If you were making a purchase, you should click Cancel to avoid a duplicate transaction. Otherwise, click Retry to display the webpage again."
Any ideas why this is happening?
This is the double-submit problem in browsers.
When a page is loaded using POST request and you try to reload the page using location.reload(true);, the browser needs to send another POST request to the server and this may cause problems as POST is supposed to change state on the server. Therefore, the browser needs confirmation from the user. To solve this problem, we usually use POST-REDIRECT-GET pattern.
In your case, just simply using location.href = location.href should solve the problem as this will reload the page using GET.
This occurs when you try to return view(Model) from your POST request. Actually you cannot return a view from POST request because returning a view is supposed to be a GET operation and it must be done under GET request.
So after posting your data successfully and saving the data in database , you have to use ReturnToAction in your controller and return your final view from that action method.
Also If you want to refresh your page, you must use location.href = location.href instead of window.reload(), because location.href will get the data through GET request.
You can create a setTimeout function like this.
This will not give you any
setTimeout(function () {
window.parent.location.reload();
}, 100);
The Alert Message shows when refreshing a page in IE by using
That works... When you want to refresh the parent page.
This might be a valid soultion:
window.opener.location.href = window.opener.location.href;
I faced the same problem while calling modal window.
I removed location.reload and just returned true value from the function.
This solved my problem.
In my case I had something totally unrelated reloading the page, someone put some javascript code to reload the page in case of resize and it was always being triggered on document.ready, making it do the post request twice, so if none of the solutions here work just make sure that there isn't some random javascript reloading the page without you knowing about it, may be useful press F12 and check the network tab to see if something unexpected is being called.
I have the following scenario:
The user fills an HTML form
The user presses the submit button
Something unexpectedly bad happens
The server retrieves an error page with a retry button. This page does not have the original form anymore. When the user clicks the retry button I want the last post data to be resubmitted
I don't even know if it's possible. I'm trying this on the retry button:
window.location.reload(true);
The result is:
On firefox it works perfectly. It reposts the data and shows the resulting HTML to the user.
On Google Chrome it does not repost the data, it kind of uses a GET on the same URL, I'll take a look at Fiddler to make sure
IE 9 reposts the data but shows a blank screen in return. If I reload it will show the proper page.
I'd like every of them to work as Firefox. I guess the problem is in the absense of the original form in this error page.
Is there anything I can do in JavaScript to make them all have the proper behavior?
You should include the post data in the retry form. Wrap the post data in a <form> and resubmit the form.
Form:
<form id="retryform" name="retryform" action="postfile.html" method="post">
<!-- post data -->
</form>
JS:
document.getElementById('retryform').submit();
// or
document.forms["retryform"].submit();
// or
document.retryform.submit();