Search code examples
javascriptformsbrowser-history

In Javascript, can I “clear” the browser (history -1) after a form submission?


I have a site with two pages: page A with a form and a submit button; page B that shows results of computation, based on data from page A.

I want to submit the form data with a POST, so that I can't use $.post or whatever;

I can't use document.location.replace('ULRPageB?param1=value1&...&paramN=valueN') because the parameters must be hidden from query string.

But I have to ensure that if the user clicks the browser BACK button from the page B, he goes two page back, not just one.

The assumptions cannot be modified: in other word the question is:

exist a way to do a GET not AJAX to a servlet, replacing the history: is document.location.replace.

Exist an equivalent way to do a POST not AJAX to a servlet that replace the history?


Solution

  • There's the Post/Redirect/Get pattern

    1. The browser submits the form using HTTP POST.
    2. The server sets the Location HTTP response header field.
    3. The browser makes a HTTP GET request to the page mentioned in the Location HTTP response header field

    This makes the back button behave more user friendly because it does not got back to the page that accepted the POST (hence no "POST data has expired" warning and no accidental duplicate submission). Rather, the back button goes back to the page that showed the form.

    In this scenario, the back button technically goes back two pages. If you want to go back one page further: don't do that, it will upset your users.