7

In my project I am using below Javascript code history.back(); for gooing back to previous page.(like back arrow on window). This function is working fine on IE and Firefox but not on google crome?

<input type="button" value="Go back" onclick="history.go(-1); return false" />

I get the error bellow

Confirm Form Resubmission

This web page requires data that you entered earlier in order to be properly displayed. You can send this data again, but by doing so you will repeat any action this page previously performed. Press Reload to resend that data and display this page.

Did googling but everybody is suggesting history.go(-1);return false; but it does not work either. Also tried history.back() but that does not help too

php-dev
  • 6,998
  • 4
  • 24
  • 38
emilly
  • 10,060
  • 33
  • 97
  • 172

3 Answers3

6

The previous page was displayed after a POST request (usually occurs when submitting a form). That means, to display the previous page, the form data, which was submitted in the POST request, must be submitted again. It has nothing to do with your history.go(-1) of Javascript, it will also occur when you press the back button of your browser.

You can use the Post Redirect Get pattern do work around this problem.

You can also use GET on your form instead:

<form action="..." method="GET">

However, do not use this for forms where data is added on your server, or it will be submitted everytime the user the back button and comes back to this page. See also: When should I use GET or POST method?

Community
  • 1
  • 1
Uooo
  • 6,204
  • 8
  • 36
  • 63
  • any example how to use PRG? – wakqasahmed Jun 20 '13 at 09:10
  • 1
    @wakqasahmed Example code depends on the used language on server side (PHP, Java,...). The Wikipedia Link I provided gives information what the pattern is about. Examples of how to implement this in several languages can be found on the internet already. If someone has trouble with implementing it, he or she can ask for help here again. – Uooo Jun 20 '13 at 09:12
  • @w4rumy if this was the case why does it work on all browsers except chrome..? – Bhavik Jun 20 '13 at 09:21
  • @Bhavik It could be that other browsers keep an "internal" cache of the displayed webpage. You can argue now which browser is right and which is wrong, but if you want to implement it correct on server side, you should use PRG. You can also use GET as alternative to POST form submission - I added this alternative in my answer. – Uooo Jun 20 '13 at 09:28
  • @w4rumy I am not sure how PRG will help here. Can you eloborate that? – emilly Jun 20 '13 at 11:12
  • @emilly Am I correct that the page you would see when going back in history, is a page which was originally displayed after a form was submitted? – Uooo Jun 20 '13 at 11:20
  • Yes. here is the flow. Page1---(Post Request)--> Page2---(Post Request)--> Page3(is the page where i am doing history.back() and want to go back to page 2). – emilly Jun 20 '13 at 11:52
  • @emilly Yes, the PRG pattern will help you here. As said, it also occurs when using the back button in your browser. Did you understand why the post requests are causing you problems and how PRG can solve them? – Uooo Jun 20 '13 at 11:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32077/discussion-between-w4rumy-and-emilly) – Uooo Jun 20 '13 at 12:06
0

Try this:

<script>
function goback() {
    history.go(-1);
}
</script>
<a href=”javascript:goback()”>Back</a>

It works in Chrome and other browsers.

The above was copied verbatim from:

http://simonthegeek.com/technical/history-back-problems-with-chrome-and-safari/

Moustafa Elqabbany
  • 1,110
  • 9
  • 10
-1

I would prefer client side code instead of harrasing my server
I tried this and it worked on all browsers:

<button onclick="javascript:window.history.back()">Back</button>  

EDIT
Alternately you can use web cache to store which was your last page and then you can easily redirect on that page..

Bhavik
  • 4,836
  • 3
  • 30
  • 45