DEV Community

Cover image for Smart solutions to avoid JS problems and hacks

Smart solutions to avoid JS problems and hacks

faisal khan on December 05, 2020

In this day and age being a developer and being in a fast-paced environment we have to deliver as quickly as possible. While trying to deliver qui...
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 :)

Collapse
 
danielcamargo profile image
Daniel Camargo

that is awesome! thanks for sharing

Collapse
 
faisalpathan profile image
faisal khan

Thanks Daniel, keep sharing it with your dev friends.

Collapse
 
saraogipraveen profile image
Praveen Saraogi

Very helpful and relatable. Bookmarked

Collapse
 
faisalpathan profile image
faisal khan

Thanks mate, glad you liked it, keep sharing praveen

Collapse
 
nikhilmwarrier profile image
nikhilmwarrier

Awesome!

Collapse
 
faisalpathan profile image
faisal khan

Thanks Nikhil, keep sharing it with your dev friends.

Collapse
 
ronaksonigara profile image
Ronak Sonigara

Great article 👍

Collapse
 
faisalpathan profile image
faisal khan

Thanks ronak, appreciated

Collapse
 
sarahcodes_dev profile image
Sarah 🦄

Awesome collection of tips 👏🏻

Collapse
 
faisalpathan profile image
faisal khan

Thanks Sarah, keep sharing it with your dev friends.

Collapse
 
kkyusufk profile image
kkyusufk

Learnt a lot from this! Looking forward to your next article buddy :)

Collapse
 
faisalpathan profile image
faisal khan

Thanks yusuf, always keep learning