How to maintain the scroll of page we have visited previously using localStorage

April 7, 2022 · 1 min read

Here’s the Code that I have used for maintaining the scroll of web page I have visited Previously using localStorage.

$(function () {
/scroll maintaining states/
var storedResult = localStorage.getItem("location");
var storedURL = localStorage.getItem("url");
if (storedURL !== 'undefined' && storedResult !== null) {
if (storedResult !== 'undefined' && storedResult !== null) {

$(window).scrollTop(storedResult);
}
}
$(window).scroll(function () {
var scrolledDown = window.scrollY;
var currentUrl = window.location.href;
localStorage.setItem("location", scrolledDown);
localStorage.setItem("url", currentUrl);
});

});

How to get SITE URL in javascript
How to check checkbox is checked or not using jquery
Share on Facebook
Facebook
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
Pin on Pinterest
Pinterest