Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

history.js - History API: Javascript Pushstate, get previous URL

$(window).bind('statechange',function(){
     var State = History.getState(),
     url = State.url;
});

In the following function, url returns the current URL. What I'm looking for is, within that function, to get the previous URL, so for example, if I'm on /cars/ferrari and go to /cars/ford, url would return cars/ford, but I want to get /cars/ferrari.

How do I get the previous URL within a statechange event?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
$(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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...