Error when using Javascript window.history.back () [duplicate]

0

Hello, I am having trouble using the window.history.back () method in Javascript, it has the functionality to return to the previous page of my application, the problem is that I have a POST-type search on this page and when I try to return to the previous page the system breaks because I try to resend this form.

Code used:

<button class="tiny" onclick="javascript: window.history.back();" name="btn-back-page">Voltar</button>

Error message:

    
asked by anonymous 01.06.2018 / 19:53

1 answer

1

Yes indeed, the browser will always confirm when you are returning to a page that was the result of a POST. It may not be your case on this page, but it could be a shopping page and there would be a risk for the user to send the card twice, a purchase, etc.

It's not cool to just try to "circumvent" the browser's security accordingly. There are some people who try to use location , but I do not think it's the ideal solution.

If you want to make a website more robust and need to use POST, read about Post / Redirect / Get

In short, this strategy makes you work like this, to ensure that the user can use the browser's return (and consequently your script too) without major problems:

  • Whenever the server receives a POST, instead of returning the response directly, save the variables and return a 302 (a response header for the browser to redirect to another page)
  • This new page works with GET, using the already set variables
  • If the user presses the back, it will return to the GET page
01.06.2018 / 20:15