window.location.replace()
can issue a GET request to target URL and replace the browser "back" history.
Is there any method to issue a POST request to target URL and replace the browser "back" history as well (with/ without jQuery is fine)?
Currently using the following code to issue a POST request, but it does not replace the "back" history:
var actionForm = $('<form>', {'action': 'register.php', 'method': 'post'}).append($('<input>', {'name': 'action', 'value': 'user_register', 'type': 'hidden'}));
actionForm.appendTo('body').submit();
The replace() method of the Location interface replaces the current resource with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session History , meaning the user won't be able to use the back button to navigate to it.
Window location. The replace() method replaces the current document with a new one.
window. location is an object that holds all the information about the current document location (host, href, port, protocol etc.). location. href is shorthand for window.
you might want to do something like these.
$.ajax({
url: "your url here",
type: "POST",
data: {
field1: "value1",
field2: "value2",
field3: "value3"
},
success: function (response) {
if (response.successFlag) {
//Replace current location from the history via history API
window.history.replaceState({}, 'foo', '/foo');
window.location = "url of target location here if you want to send a get request";
$("#form-id").submit();//if you want to post something up
}
}
});
I'm thinking maybe you would also get rid of the need for removing the history using this approach.
OR
you can use the following to replace the record of the current page from the history and submit your form.
window.history.replaceState({}, 'foo', '/foo');
$("#form-id").submit();
OR
you can use the following code when the next page loads and you want to remove the record of the next page from the history:
window.history.replaceState({}, 'foo', '/foo');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With