Prevent browser from snapping to previous scroll position when pushing back button

狂风中的少年 提交于 2019-12-10 13:05:39

问题


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!


回答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.




回答2:


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.




回答3:


I found a better solution!

Put the following code on every page:

<script>
  setTimeout('window.scrollTo(0, 0);', 1);
</script>


来源:https://stackoverflow.com/questions/11512140/prevent-browser-from-snapping-to-previous-scroll-position-when-pushing-back-butt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!