简体   繁体   English

JavaScript后退按钮无法正常工作-滚动到页面底部,而不是先前的滚动位置

[英]Javascript back button not working properly -scrolling to the bottom of the page, instead of previous scroll location

I'm using the following code on a wordpress website and it is only present on the post pages: 我在wordpress网站上使用以下代码,并且仅出现在帖子页面上:

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

What I would like the code to do is bring the user directly back to the spot where they left off on the previous page and for the most part the code is working fine. 我希望代码能够将用户直接带回上一页停止的地方,并且在大多数情况下代码可以正常工作。

Problem: After clicking forward and then clicking back a few times - the back button will continue to bring you back button, however not to the same scroll spot - it actually brings you back to the bottom of the webpage and will keep doing this every time until you leave the website and then return. 问题:向前单击然后单击几次后,“后退”按钮将继续为您带来“后退”按钮,但是不会到达同一滚动点,实际上,它使您回到网页的底部,并且每次都会继续执行此操作直到您离开网站,然后返回。

Any help with this problem will be greatly appreciated. 任何有关此问题的帮助将不胜感激。

Here is my website address - if that helps >> http://rumpleskilton.com/djs-bands-singers/ 这是我的网站地址-如果有帮助>> http://rumpleskilton.com/djs-bands-singers/

(to see the problem - select one of the listings and then click the back button, which is just above the page header. It could take several times to see the problem. Just click a few of the listings.) (要查看问题,请选择其中一个列表,然后单击页面标题上方的后退按钮。这可能要花几倍的时间才能看到问题。只需单击几个列表即可。)

have you try this way ? 你尝试过这种方式吗?

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

i hope it's help you. 希望对您有帮助。

I believe you are using Chrome. 我相信您正在使用Chrome。 Chrome is known to demonstrate such behaviour. 众所周知,Chrome可以证明这种行为。 Try with Firefox and MS Edge and see if you get the same result (I trust you won't). 尝试使用Firefox和MS Edge,看看是否得到相同的结果(我相信不会)。

In any case, the safest method is to use the page URL to navigate to, instead of back() . 无论如何,最安全的方法是使用页面URL导航到而不是back() Alternatively, you could save the last page name in the url (?lastpage=index or whatever) and change your back() function to grab that page name and navigate there. 或者,您可以将最后一个页面名称保存在url中(?lastpage = index或其他内容),然后更改back()函数以获取该页面名称并在那里导航。

Trusting the browser behaviour is not your friend in this situation. 在这种情况下,信任浏览器的行为不是您的朋友。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows滚动可滚动到顶部而不是底部 - Windows scroll working on scrolling to the top instead of bottom Javascript滚动到滚动页面中结果的底部 - Javascript scroll to bottom of results within a scrolling page Javascript-单击后退按钮时,将页面滚动到上一页的最后位置 - Javascript - Scroll page to last position of previous page when the back button is hit 纯javascript自动滚动到页面底部,然后备份 - pure javascript auto scroll to bottom of a page then back up 为什么在我滚动底部时无限滚动不起作用,而是在滚动顶部触发? - Why is Infinite scrolling not working when I scroll bottom, instead it triggers on scroll top? 单击浏览器的“后退”按钮时,返回上一页的上一个位置 - Go Back to the Previous Location on Previous Page when Clicking Browser Back Button JS平滑滚动,滚动到最终/底部div的底部而不是顶部 - JS Smooth scrolling, scroll to bottom of final / bottom div instead of top 永无止境的滚动页面(滚动页面的顶部和底部) - never ending scrolling page (scrolling top and back to bottom of the page or otherwise) 如果当前滚动位置在底部附近,则javascript滚动到div的底部 - javascript scroll to bottom of div if current scroll location is near bottom 检查时的javascript滚动到页面底部 - javascript on check scroll to bottom of page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM