Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Advanced Search
  • 3

How to return to the same scroll position when using browser's back button?


LIGHTBOXQUESTION

Question

Hello, can you please help?
I would simply like to have my site do the following:

1.¬†When you click the back button in a browser, it takes you back to where you were on the previous page. Currently, it takes you back to top of page, which is frustrating if you were 10 images down in a gallery or on mobile. I am okay with a load time... ūüôā

Is this possible on Squarespace? Example can be found here:
https://www.nicholaskonert.com/art

Thank you for your help and support.

Link to post
  • Answers 15
  • Created
  • Last Reply

Top Posters For This Question

Top Posters For This Question

Popular Posts

Hey friends,  Here's a snippet of code that I use to save the scroll position. I use it when a user has navigated away from a long index page and I want them to return to the same place. Not

Try this code. It works for me. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(functi

Posted Images

15 answers to this question

Recommended Posts

  • 1

I am having the same issue! 

I have many images with click through links, and I do not have them opening in a new window. However, when clicking the "back" button in my browser, it returns to the previous page but scrolled all the way to the bottom. Very annoying to find your place again.

Attached are images describing this, and here is the site: https://kristinspiewak.com/#/furniture/

Any solutions to this??

squarespace-issue.jpg

Link to post
  • 0

Hey friends, 

Here's a snippet of code that I use to save the scroll position. I use it when a user has navigated away from a long index page and I want them to return to the same place.

Note, for an index, you must add it in the code injection for the INDEX, not the page section. 

<script type="text/javascript">
  function refreshPage () {
      var page_y = document.getElementsByTagName("body")[0].scrollTop;
      window.location.href = window.location.href.split('?')[0] + '?page_y=' + page_y;
  }
  window.onload = function () {
      setTimeout(refreshPage, 35000);
      if ( window.location.href.indexOf('page_y') != -1 ) {
          var match = window.location.href.split('?')[1].split("&")[0].split("=");
          document.getElementsByTagName("body")[0].scrollTop = match[1];
      }
  }
</script>

Let me know if this helps!

Link to post
  • 0
On 6/18/2020 at 2:08 PM, colin.simmons said:

Hey friends, 

Here's a snippet of code that I use to save the scroll position. I use it when a user has navigated away from a long index page and I want them to return to the same place.

Note, for an index, you must add it in the code injection for the INDEX, not the page section. 


<script type="text/javascript">
  function refreshPage () {
      var page_y = document.getElementsByTagName("body")[0].scrollTop;
      window.location.href = window.location.href.split('?')[0] + '?page_y=' + page_y;
  }
  window.onload = function () {
      setTimeout(refreshPage, 35000);
      if ( window.location.href.indexOf('page_y') != -1 ) {
          var match = window.location.href.split('?')[1].split("&")[0].split("=");
          document.getElementsByTagName("body")[0].scrollTop = match[1];
      }
  }
</script>

Let me know if this helps!

I'm confused what I'm supposed to do with this code. Where do I paste it? In the "Code Injection" section?

Link to post
  • 0
On 9/16/2020 at 11:50 PM, kylejcho said:

I'm confused what I'm supposed to do with this code. Where do I paste it? In the "Code Injection" section?

Add to Home > Settings > Advanced > Code Injection > Footer

Email me if you have need any help (free, of course :-D). Answer within 24 hours.

How to:

-- Yup!

Link to post
  • 0

Try this code. It works for me.

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

<script type="text/javascript">
$(document).ready(function () {

    if (localStorage.getItem("my_app_name_here-quote-scroll") != null) {
        $(window).scrollTop(localStorage.getItem("my_app_name_here-quote-scroll"));
    }

    $(window).on("scroll", function() {
        localStorage.setItem("my_app_name_here-quote-scroll", $(window).scrollTop());
    });

  });
</script>

 

Link to post
  • 0

It is odd, but I have never really noticed where my site goes when I hit the browser back button when scrolled down the site. I tried this in a page with summary blocks, a  gallery page image I accessed from an index thumbnail and a blog entry, they all go right back to where I was at when I scrolled down.  I have done nothing to make that happen, but I like that it does. It will not do it when viewing my site as the owner ony as a visitor. 

my random views

Edited by derricksrandomviews
Link to post
  • 0
On 12/25/2020 at 8:01 PM, Helen_Phan said:

Try this code. It works for me.


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

<script type="text/javascript">
$(document).ready(function () {

    if (localStorage.getItem("my_app_name_here-quote-scroll") != null) {
        $(window).scrollTop(localStorage.getItem("my_app_name_here-quote-scroll"));
    }

    $(window).on("scroll", function() {
        localStorage.setItem("my_app_name_here-quote-scroll", $(window).scrollTop());
    });

  });
</script>

 

Helen, Where do I drop this code in Header, Footer or Order Conformation sections

Link to post
  • 0

My site has a lot of scrolling. The template Avenue returned you to where you were on the page. I switched to Brine template to have more control on the post. But I found out this annoying thing where back arrows bring you to the top of the page. I can imaging this will get old to the users experience. I am about to inject the code above but am uncertain as to which section. Also i am nervous that I will break my site. If something bad happens can I simply go in and remove it?

Link to post
  • 0

It worked except when you click on a thumbnail it drops you off at the bottom of the posts. Any way around this? Also it seemed to work in placed in the Header and Footer. Is there one that is better?

Link to post
  • 0
On 6/18/2020 at 11:08 AM, colin.simmons said:

Hey friends, 

Here's a snippet of code that I use to save the scroll position. I use it when a user has navigated away from a long index page and I want them to return to the same place.

Note, for an index, you must add it in the code injection for the INDEX, not the page section. 


<script type="text/javascript">
  function refreshPage () {
      var page_y = document.getElementsByTagName("body")[0].scrollTop;
      window.location.href = window.location.href.split('?')[0] + '?page_y=' + page_y;
  }
  window.onload = function () {
      setTimeout(refreshPage, 35000);
      if ( window.location.href.indexOf('page_y') != -1 ) {
          var match = window.location.href.split('?')[1].split("&")[0].split("=");
          document.getElementsByTagName("body")[0].scrollTop = match[1];
      }
  }
</script>

Let me know if this helps!

This one worked great. I land at the top of each post and don't loose the spot on scrolls. This was especially important on smartphones. One question; can Ajax be enabled with this script. I am afraid to try it.

Link to post

Create an account or sign in to comment

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


√ó
√ó
  • Create New...