History API: Javascript Pushstate, get previous URL

后端 未结 3 917
暗喜
暗喜 2020-12-19 12:14
$(window).bind(\'statechange\',function(){
     var State = History.getState(),
     url = State.url;
});

In the following function, url returns th

相关标签:
3条回答
  • 2020-12-19 12:33

    All I would do is push the URL on to the state object so you can get it back again.

    So this when you use pushState :

    history.pushState(
      {'previous_url': document.location.href}, 
      document.title,
      "?some_query_string"
    );
    

    Then when you get the state it will have previous_url on it :)

    0 讨论(0)
  • 2020-12-19 12:38
    $(window).bind('statechange',function(){
        // Prepare Variables
        var State = History.getState(),
            url = State.url,
            states = History.savedStates,
            prevUrlIndex = states.length - 2,
            prevUrl = states[prevUrlIndex].hash;
    });
    

    That seems to do the trick! prevUrl gives me the desired URL.

    0 讨论(0)
  • 2020-12-19 12:47

    Try this:

    alert(document.referrer);
    

    Note:

    The value is an empty string if the user navigated to the page directly (not through a link, but, for example, via a bookmark). Since this property returns only a string, it does not give you DOM access to the referring page.

    See more here: https://developer.mozilla.org/en-US/docs/DOM/document.referrer?redirectlocale=en-US&redirectslug=document.referrer

    0 讨论(0)
提交回复
热议问题