最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

Like the title says, I'd like to be able to perform a different onstatechange event if the pushState function is called, instead of the back function. Or, if the go function is negative or positive.

Example:

if History.pushState() or History.go(1) are called, I want the statechange event's callback to be forwardPushState

if History.back() or History.go(-1) are called, I want the statechange event's callback to be backwardsPushState

Like the title says, I'd like to be able to perform a different onstatechange event if the pushState function is called, instead of the back function. Or, if the go function is negative or positive.

Example:

if History.pushState() or History.go(1) are called, I want the statechange event's callback to be forwardPushState

if History.back() or History.go(-1) are called, I want the statechange event's callback to be backwardsPushState

Share Improve this question edited Feb 16, 2020 at 10:18 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 14, 2013 at 16:20 AschererAscherer 8,1013 gold badges45 silver badges60 bronze badges 7
  • 1 Keep track of the last visited page, if the current page redirects to the last visited page, chances are the direction is backwards. – adeneo Commented May 14, 2013 at 16:21
  • It will be better if you illustrate by an example. regards – Adib Aroui Commented May 15, 2013 at 0:25
  • PushState pushes a new state in the stack (a state is some data stored after a new action). It has no relationship with back and go. Back and go are functions to navigate over pushed states in stack. I say this because in your edit, it looks like you are thinking that pushState and go(1) are equivalent. – Adib Aroui Commented May 18, 2013 at 23:22
  • I know they aren't the same, just giving an example of what i was looking for – Ascherer Commented May 20, 2013 at 17:26
  • 1 Please to follow this topic maybe it will be useful for you. There is an answer there about how to track the onstatechange event and ....stackoverflow./questions/16692314/… – Adib Aroui Commented May 29, 2013 at 14:54
 |  Show 2 more ments

2 Answers 2

Reset to default 7 +25

A state is some data related to a page (as the user see it in the browser). If the user wants to be in a certain page, this page is the same, either he is ing from back button click or from forward button click.

PushState pushes a new state in the stack. It has no relationship with back and go. Back and go are functions to navigate over pushed states in stack. I say this because in your edit, it looks like you are thinking that pushState and go(1) are equivalent.

Maybe if you want to know from which direction the user is ing, you should analyse onstatechange event to know if it takes any parameter that stores direction, which is not a trivial task IMO.

The main thing I think, is that it has no relationship with the go (-1) or go(1) or back.

Maybe this will work for you.

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

The code above will make it difficult to use the back button to load this page.

Second try

The code below was taken from another stack overflow.

detect back button click in browser

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;   
        }
    };
}

}

发布评论

评论列表(0)

  1. 暂无评论