简体   繁体   中英

Chrome beforeunload Event and history API

I have thus code to store the scroll position and height of a responsive page with a drawer menu.

$(window).on('beforeunload', function (e) {
                if (window.history && window.history.replaceState) {
                    var $formContainer = $('#Form1'),
                        $drawerContent = $('#drawer-content'),
                        pageState = {
                            page: window.location.href,
                            formContainerHeight: $formContainer.height(),
                            drawerContentScrollTop: $drawerContent.scrollTop()
                        };
                    console.log(pageState, e.type)
                    window.history.replaceState(pageState, window.document.title);
                }
            });

On page load I console.log the window.history.state to ensure page size when going back / forward in history.

I get the expected values in IE10+, FireFox and Opera, but chrome seems to log the values from the page in history +1.

You shouldn't Hack so hard, and messing with the title
Javascript provides a set of nice methods to handle prev/next while actually staying on the same page:
Namely location.hash ( Fragment Identifier ).
Use them instead: www.whatever/index#125,615 where x,y are your values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM