After scrolling down and clicking on back button takes back to top of page - on mobile only

After scrolling down and clicking on back button takes back to top of page - on mobile only

Martin2007
Visitor
1 0 0

After scrolling down the screen when using a mobile you find the collection you want to view. Once view you click the back button which annoyingly takes you back to the top of the page again meaning you have to scroll all the way down again to continue from where you left off. I saw this code to add but it doesnt work. Any other ideas?

<script>
if (window.location.pathname === '/') {
if (localStorage.getItem("my_app_name_here-quote-scroll") != null) {
const savedScrollPosition = localStorage.getItem("my_app_name_here-quote-scroll");
window.scrollTo(0, savedScrollPosition);
}
window.onscroll = function() {
localStorage.setItem("my_app_name_here-quote-scroll", window.scrollY);
};
}
</script>

Replies 2 (2)

Dev-Brad
Shopify Partner
22 0 0

Hey @Martin2007 

 

Did you have a website link to take a further look into this? 

I noticed there is a conditional to check the path name is '/'

Could you test this with some console logs and see if this is working

New code to trial: 

<script>
console.log("Checker 1: Script started");

if (window.location.pathname === '/') {
console.log("Checker 2: On homepage");

const savedScrollPosition = localStorage.getItem("my_app_name_here-quote-scroll");

if (savedScrollPosition != null) {
console.log("Checker 3: Found saved scroll position: " + savedScrollPosition);
window.scrollTo(0, savedScrollPosition);
} else {
console.log("Checker 3: No saved scroll position found");
}

window.onscroll = function() {
localStorage.setItem("my_app_name_here-quote-scroll", window.scrollY);
console.log("Checker 4: Scroll position saved: " + window.scrollY);
};
} else {
console.log("Checker 2: Not on homepage");
}
</script>

 

 

This should give some sufficient logs for us to work with, let me know how you go and what logs you see happening 

 

7+ Years as an expert Shopify Developer | Message for a free 15 mins consultation
ProtoMan44
Shopify Partner
489 42 79

@Martin2007 can you please share link.

- A thirsty developer passionate about supporting the community. If you'd like to, you can  

Buy me a Coffee.


- Your Shopify Solution Specialist Get a Quote at

ProtoMan44

 - 

Chat On Whatsapp


Crafting exceptional e-commerce experiences to elevate your online presence. Let's build your success story together!