Chrome加载前事件和历史记录API

Chrome beforeunload Event and history API

本文关键字:历史 记录 API 事件 加载 Chrome      更新时间:2023-09-26

因此,我编写了代码,用抽屉菜单存储响应页面的滚动位置和高度。

$(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);
                }
            });

在页面加载时,我会console.log窗口.history.state,以确保在历史记录中后退/前进时页面大小。

我在IE10+、FireFox和Opera中得到了预期的值,但chrome似乎将页面中的值记录在历史+1中。

你不应该如此努力地破解,并破坏标题
Javascript提供了一组很好的方法来处理prev/next,同时实际上保持在同一页面上:
即location.hash(片段标识符)
请使用它们:www.anything/index#125615,其中x,y是您的值