就像标题说的那样,如果调用函数而不是函数,我希望能够执行不同的onstatechange事件。或者,如果函数是负数或正数。pushStatebackgo
例子:
如果History.pushState()或被History.go(1)调用,我希望statechange事件的回调是forwardPushState
如果History.back()或被History.go(-1)调用,我希望statechange事件的回调是backwardsPushState
就像标题说的那样,如果调用函数而不是函数,我希望能够执行不同的onstatechange事件。或者,如果函数是负数或正数。pushStatebackgo
例子:
如果History.pushState()或被History.go(1)调用,我希望statechange事件的回调是forwardPushState
如果History.back()或被History.go(-1)调用,我希望statechange事件的回调是backwardsPushState
状态是与页面相关的一些数据(正如用户在浏览器中看到的那样)。如果用户想在某个页面,这个页面是一样的,要么他来自后退按钮点击,要么来自前进按钮点击。
PushState 在堆栈中推送一个新状态。back它与和没有关系go。Back并且go是导航堆栈中推送状态的函数。我这样说是因为在您的编辑中,您似乎认为 pushState 和 go(1) 是等价的。
也许如果您想知道用户从哪个方向来,您应该分析onstatechange事件以了解它是否需要任何存储方向的参数,这不是一项简单的任务 IMO。
我认为主要是它与 go (-1)or go(1)or没有关系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;
}
};
}
}