Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore scroll position, form state, and focus #39

Open
adamchainz opened this issue Jan 6, 2022 · 2 comments
Open

Restore scroll position, form state, and focus #39

adamchainz opened this issue Jan 6, 2022 · 2 comments

Comments

@adamchainz
Copy link
Owner

Description

@kezabelle's django-livereloadish does this by restoring these key pieces of state after reload. It serializes the values into sessionStorage and restores them on page load.

I think this behaviour would be valuable to add here. Probably a good idea to add one at a time.

@adamchainz
Copy link
Owner Author

Firefox restores scroll position and form data, but not focus.

Chrome doesn't seem to restore any.

I'm tempted to just restore focus and document "use firefox" for the other stuff.

@boxed
Copy link

boxed commented Jul 17, 2024

I use this at work to restore scroll position:

    window.addEventListener("beforeunload", function (e) {
        sessionStorage.setItem('scroll_pos', window.scrollY);
        sessionStorage.setItem('scroll_url', window.location.href);
    });


document.addEventListener("DOMContentLoaded", function (event) {
    let scroll_pos = sessionStorage.getItem('scroll_pos');
    if (scroll_pos) {
        if (sessionStorage.getItem('scroll_url') === window.location.href) {
            window.scrollTo(0, scroll_pos);
        }
        sessionStorage.removeItem('scroll_pos');
    }
});

It works extremely well, almost gets my entire site to feel like a SPA.

Form state and focus seems more like a nice-to-have compared to scroll position imo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants