1

I am debugging a large piece of javascript and am having trouble figuring out what is going on with the browser back button as it relates to pushState() and replaceState(). I have the following prototype method:

    SolrUrl.prototype.storeQueryObject = function(data) {

    var newUrl = window.location.protocol
        + "//"
        + window.location.host
        + window.location.pathname
        + "?"
        + $.param(data);

    // window.history.pushState(newUrl);

    window.history.pushState({
        path: newUrl
    }, "", newUrl);
};

I have a faceted search that allows me to do multi-level filtering. The above works as it allows me to filter different queries and use the back button to return to previous filters but I can not use the browser 'back' button once I get back to the intial state of the search. For example, if I go to google.com -> mysite.com -> and then filter my search to "components", I can use the back button to get back to the initial state of mysite.com, but cannot use it to return to google.com.

I can partially solve this problem by changing pushState to replaceState, but this does not allow me to use the back button to go back through filter states. For example, when using replaceState in the above function. If I go to google.com -> mysite.com -> filter "components" -> filter -> "component type" and hit the back button, it takes me back to mysite.com. What am I doing wrong? How can I make the browser's back button to go back through filter states as well as previous urls?

JordanBarber
  • 2,041
  • 5
  • 34
  • 62
  • If I remember correctly, popping a state from the history stack is a specific window event that you can (and probably should) alter in some way to accommodate for any custom history state changes you're making on your website. – Nick Zuber May 06 '16 at 19:47
  • @NickZuber That makes sense, but how would I go about accommodating the history change. Basically, what you're saying makes sense but I don't know how to do it :) Any ideas? – JordanBarber May 06 '16 at 19:50
  • Check out [this question](http://stackoverflow.com/questions/25806608/how-to-detect-browser-back-button-event-cross-browser) - it may be what you're looking for – Nick Zuber May 06 '16 at 20:09
  • 1
    @NickZuber that doesn't really pertain to my issue. Is there anyway to use the console to watch the history stack as my pushState() alters the history? – JordanBarber May 06 '16 at 21:00

0 Answers0