7

I need to prevent browser from snapping to previous scroll position when the user pushed a back button like this:

<a href="javascript:history.go(-1)"
      onClick="javascript:history.go(-1)" title="< GO BACK"></a>

after pushing that button the browser will return to the previous scroll position on this page i want to stop that behavior and just load the top of the page.

Hope someone know a proper solution.

-exemple-

open a page scroll down go to a new page and hit the back button the page will auto scroll down to the place you scrolled before!

Community
  • 1
  • 1
phj
  • 123
  • 2
  • 9
  • 1
    Looks like you're scrolling using anchors. If you want to accomplish your goal, remove the anchor names and scroll to them with JavaScript's `scrollTo` (or similar) function. – Some Guy Jul 16 '12 at 20:41
  • no this is not the case! just open a page scroll down go to a new page and hit the back button the page will auto scroll down to the place you scrolled before! – phj Jul 16 '12 at 20:51

3 Answers3

1

I'm fairly certain the behavior you're describing is is best classified as one of those things considered to be a user preference. (One of those things that you're not meant to tamper with)

@mrtsherman came up with a hack/workaround for this, but unless it's seriously breaking your webapp's usability, I think you should let the browser behave as the user would normally expect it to behave (and scroll to the position they were in when they left the page). Be sure to upvote mrtsherman for his sweet nugget of js if you use it.

Community
  • 1
  • 1
MicronXD
  • 2,190
  • 16
  • 24
  • 1
    thank you so much didn't saw the hack before i will consider using it for some pages – phj Jul 16 '12 at 21:30
0

I have the same problem and I haven't find yet the correct solution,
but I can give you a very good workaround for this problem.

Just scroll to the top before load the next page.
When the user click back, the browser will scroll to the top.

Example:

First Page:
<a href="page2.htm" onclick="window.scrollTo(0, 0);">Next</a>

Second Page:
<a href="page1.htm" onclick="history.go(-1); return false;">Back</a>

Note that href="page1.htm" loads only if user select open in new window/tab

Excuse my English. I hope this helps.

ilias_gr
  • 145
  • 2
  • 10
-4

I found a better solution!

Put the following code on every page:

<script>
  setTimeout('window.scrollTo(0, 0);', 1);
</script>
ilias_gr
  • 145
  • 2
  • 10