是否有一种方法可以告诉我们国家将朝着什么方向发展?

Is there a way to tell what direction the state is going with history.js?

本文关键字:国家 我们 什么 方向 一种 方法 是否      更新时间:2023-09-26

就像标题所说的那样,如果调用pushState函数而不是back函数,我希望能够执行不同的onstatechange事件。或者go函数为负或正

的例子:

如果History.pushState()History.go(1)被调用,我希望statechange事件的回调是forwardPushState

如果History.back()History.go(-1)被调用,我希望statechange事件的回调是backwardsPushState

状态是与页面相关的一些数据(正如用户在浏览器中看到的那样)。如果用户想要进入某个页面,这个页面是一样的,他要么点击后退按钮,要么点击前进按钮。

PushState向栈中推送一个新状态。与backgo没有关系。Backgo是在堆栈中的push状态中导航的函数。我这么说是因为在你的编辑中,看起来你认为pushState和go(1)是等价的。

也许如果你想知道用户从哪个方向来,你应该分析onstatechange事件,知道它是否需要任何存储方向的参数,这不是一项微不足道的任务。

我认为主要的事情是,它与go (-1)go(1)back没有关系。

也许这对你有用。

<html>
<body onunload="disableBackButton()">
<h1>You should not come back to this page</h1>
</body>
<script>
function disableBackButton()
{
   window.history.forward();
}
setTimeout("disableBackButton()", 0);
</script>
</html>

上面的代码将使使用后退按钮加载此页面变得困难。

第二次尝试

下面的代码来自另一个堆栈溢出。

在浏览器中检测返回按钮点击

window.onload = function () {
if (typeof history.pushState === "function") {
    history.pushState("jibberish", null, null);
    window.onpopstate = function () {
        history.pushState('newjibberish', null, null);
        // Handle the back (or forward) buttons here
        // Will NOT handle refresh, use onbeforeunload for this.
    };
}
else {
    var ignoreHashChange = true;
    window.onhashchange = function () {
        if (!ignoreHashChange) {
            ignoreHashChange = true;
            window.location.hash = Math.random();
            // Detect and redirect change here
            // Works in older FF and IE9
            // * it does mess with your hash symbol (anchor?) pound sign
            // delimiter on the end of the URL
        }
        else {
            ignoreHashChange = false;   
        }
    };
}

}

相关文章: