DEV Community

Discussion on: Smart solutions to avoid JS problems and hacks

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Since the browser will always scroll to the previous scroll position and not on the top as expected.

No. The expected behaviour when you click 'back' is to be returned to the place from where you went 'forward' - scroll position and all. If you prevent this from happening, you are breaking the normal, expected behaviour of the browser. You shouldn't do this, and it is extremely irritating for the user

Collapse
 
faisalpathan profile image
faisal khan • Edited

Thanks Jon for commenting, the situation which i have discussed here is different, hence i mentioned not scroll to the top as expected.

For eg:
You have a header and a footer in between you have multiple small components and all the data is powered by making API calls its dynamic in nature.

Let us say you click on a small component which is just above the footer and you navigated to a new page, on the new page when you click on back you come back to the page and your scroll would be near the footer.

Since the page is very dynamic and we make API call every time the page loads and till the API has finished we show a loader or shimmer, since the page has scrolled down near the footer the user does not know whats happening above, then new data would get loaded, hence confusing the user more.

I agree with your point completely that resetting the scroll position is extremely irritating for the user, its a valid argument for page which is not very dynamic.

That is the reason by default browser set window.history.scrollRestoration to auto in order to maintain the user behaviour, but yes the case i have mentioned is a different one where we need to reset it to manual.

Collapse
 
jonrandy profile image
Jon Randy 🎖️

If this causes an issue for your page, you're doing things wrong

Thread Thread
 
faisalpathan profile image
faisal khan

No problem, i hope you write a good article on how to resolve this in future so that i can look and make things right. We are here to learn always :)