【问题标题】:Chrome beforeunload Event and history APIChrome beforeunload 事件和历史 API
【发布时间】:2014-11-08 17:46:12
【问题描述】:

因此,我有代码来存储带有抽屉菜单的响应页面的滚动位置和高度。

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

在页面加载时,我控制台记录 window.history.state 以确保在历史记录中返回/前进时的页面大小。

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

【问题讨论】:

    标签: javascript html google-chrome onbeforeunload html5-history


    【解决方案1】:

    你不应该这么用力地破解,并弄乱标题
    Javascript 提供了一组很好的方法来处理上一个/下一个,同时实际停留在同一页面上:
    即 location.hash (Fragment Identifier)。
    改用它们:www.whatever/index#125,615 其中 x,y 是您的值

    【讨论】:

      猜你喜欢
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      相关资源
      最近更新 更多