HTML5 pushState using History.js. Problems getting data from State.data

I can set the data in State.data of History.js, for example:

var pushStateData = {}; function RetrieveSearchResults(type, url, searchData) {//, showResetButton, controlToFocus, navDirection) { pushStateData = { SearchType : type, SearchData : searchData, }; RetrievePageResults(true, url, pushStateData); } function RetrievePageResults(pushNewUrl, url, pushStateData) { navigationInProgress = true; if (pushNewUrl) { if (window.History) { window.History.pushState(pushStateData, null, url); } $.get(url, pushStateData.SearchData, function (reply) { $("#search-results").html(reply); navigationInProgress = false; }); } 

If I set a breakpoint in window.History.pushState, in Chrome I clearly see that pushStateData has the right values.

However, when I try to get the data:

 $(window).bind("statechange", function (e) { if (!navigationInProgress) { var State = window.History.getState(); if (window.console && window.console.log) { console.log("popstate", State, window.location.href); } RetrievePageResults(false, State.cleanUrl, State.data); } }); 

When I set a breakpoint in the RetrievePageResults statement, the State.data object no longer has the values ​​that I set. State.data is defined and not null, but it is an empty object with no visible values.

Thanks Scott

+7
source share
1 answer

I see nothing wrong with State.data when you call pushState, make sure you call the History.js method:

 History.pushState({state:1}, "State 1", "?state=1"); 

Where:

  • First parameter => Data
  • Second parameter => Name
  • Third parameter => URL

It seems that you are not passing state, and data will only be present when you call History.pushState. When you visit the URL directly (/? State = 1), you will not have data in the state, the data will be available only when navigating back / forward when you click the state through History.pushState.

Note: make sure the navigationInProgress variable is fixed, you do not want it to be stopped there. Reset when the $ .get request failed while listening for the error callback. And when your pushNewUrl is false, the reset attribute is navigationInProgress.

+5
source

All Articles