Jump to content

Return to Place on Scroll After Back Arrow

Recommended Posts

Hello, Once again script has stopped working on my site. It is one of the most important features. I guess every time they do an update? Because there is a lot of scrolling with many individual posts I need the user to return to where they were on the posts page when they click the back arrow. I found script that works BUT only after the 2nd click back.  I need it to happen every time. Is there a way to alter this or does anyone have script that would work.? 

Thanks!

Here It is:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<script type="text/javascript">
if (window.performance && window.performance.navigation.type === window.performance.navigation.TYPE_BACK_FORWARD) {
   if (localStorage.getItem("my_app_name_here-quote-scroll") != null) {
        $(window).scrollTop(localStorage.getItem("my_app_name_here-quote-scroll"));
    }
} else {
   $(window).scrollTop();
}
window.addEventListener('beforeunload', function (e) {
        if (window.performance && window.performance.navigation.type === window.performance.navigation.TYPE_BACK_FORWARD) {
        localStorage.setItem("my_app_name_here-quote-scroll", $(window).scrollTop());
        } 
 });
</script>

 

Link to comment
  • Replies 0
  • Views 891
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.