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();
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();
Share
Improve this question
asked Aug 10, 2015 at 2:58
RaptorRaptor
54.3k47 gold badges248 silver badges399 bronze badges
1
- 1 please check my answer below, it should work if you are open to use AJAX request. – Allan Chua Commented Aug 10, 2015 at 3:16
1 Answer
Reset to default 3you 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');