0

I'm very new to JavaScript. I'm trying to make an HTML button to do 2 things (at the same time) when clicked:

1) Go back while retaining user inputs (there is a form in the previous page).

2) Go to the top of the previous page.

Here is what I have:

<input type="button" value="Go Back" onclick="javascript:history.go(-1); location.href='#'" />

UPDATE:

<script type="text/javascript">
function goBack() {
    window.history.go(-1);
    window.location.href = "#";
}
</script>

<input type="button" value="Go Back" onclick="goBack()">

The button does not do anything when clicked though.

k_rollo
  • 5,304
  • 16
  • 63
  • 95

2 Answers2

1

This is what the code does:

1) Go back while retaining user inputs (there is a form in the previous page).

2) Go to the top of the current page.

So, the first action is cancelled by the second action.

What you would need is to do the second step in the target page, i.e. code on the previous page that goes to the top when the page loads.

In the current page you would have just:

<input type="button" value="Go Back" onclick="javascript:history.go(-1);" />

In the previous page you would have a script:

window.onload = function(){
  window.scrollTo(0, 0);
};

(The script would naturally also run when the page initially loads (before it's the "previous page"), but that's not a problem because then the page is already at the top.)

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Ah yes, the "previous" page has become the "current" page on Step 2. Is it possible to just call a function on the `onclick` attribute that does the 2 things already? – k_rollo Apr 05 '15 at 09:40
  • 1
    @ohtph: The problem is that the previous page hasn't become the current page yet. When the code runs the current page is still the current page. You can't do anything in the current page to the next page that will be loaded (which is the previous page in this case), you have to do that after the page has loaded. – Guffa Apr 05 '15 at 09:50
  • You are correct, I tried it with a function and the results are still the same (see update). Might you kindly update your answer with a code sample? It would be much appreciated. :) – k_rollo Apr 05 '15 at 09:59
  • @ohtph: I added an example. – Guffa Apr 05 '15 at 10:04
  • Hi @Guffa, it goes back to the previous page with history now, but it does not jump to the top. I put your JS inside ` – k_rollo Apr 05 '15 at 10:14
  • 1
    Some browsers won't trigger the load event on back, see: http://stackoverflow.com/questions/2638292/after-travelling-back-in-firefox-history-javascript-wont-run – Guffa Apr 05 '15 at 10:20
  • Oh, I see now. I'm using Chrome by the way. I tried the suggested workaround of putting `window.onunload = function() {};` below your code but it did nothing either. I am upvoting your answer though, because you did explain the "previous vs. current" page I was confused about. Thanks for your help. :) – k_rollo Apr 05 '15 at 10:31
0

As an alternative, I just placed a "Back to Top" button on the previous page to ensure it works on all browsers.

k_rollo
  • 5,304
  • 16
  • 63
  • 95