Very good and simple solution. I made a few changes. I replaced localstorage with sessionstorage and cleared it using sessionstorage.removeitem('scrollpos') after the scroll position.