Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.location.replace - POST version?

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();
like image 647
Raptor Avatar asked Aug 10 '15 02:08

Raptor


People also ask

Why is the location replace () method preferred over the location assign () method?

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.

What does Window location replace do?

Window location. The replace() method replaces the current document with a new one.

What is difference between window location or location?

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.


1 Answers

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');
like image 85
Allan Chua Avatar answered Sep 29 '22 07:09

Allan Chua