Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 pushState using History.js. Trouble retrieving data from State.data

I can set data into the State.data of History.js, like this:

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 on the window.History.pushState statement, in Chrome, I can clearly see pushStateData has the desired values.

However, when I try to retrieve 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 on the RetrievePageResults statement, The State.data object no longer has any of the values I set. State.data is defined, and is not null, but it is an empty object without any apparent values.

Thanks, Scott

like image 804
sveatch42 Avatar asked Feb 29 '12 17:02

sveatch42


People also ask

What is history pushState ()?

In an HTML document, the history. pushState() method adds an entry to the browser's session history stack.

What object contains the html5 history API methods?

The DOM Window object provides access to the browser's session history (not to be confused for WebExtensions history) through the history object. It exposes useful methods and properties that let you navigate back and forth through the user's history, and manipulate the contents of the history stack.

What is history replaceState?

replaceState() The History. replaceState() method modifies the current history entry, replacing it with the state object and URL passed in the method parameters. This method is particularly useful when you want to update the state object or URL of the current history entry in response to some user action.

How do I check stack history?

history object allows you to access the history stack of the browser. To navigate to a URL in the history, you use the back() , forward() , and go() methods. The history. length returns the number of URLs in the history stack.


1 Answers

I don't see anything wrong with State.data, when you issue a pushState, make sure you call the History.js method:

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

Where:

  • First Parameter => The data
  • Second Parameter => The name
  • Third Parameter => The url

It seems your not passing the state, and the data will only be present if and only if you call the History.pushState. When your visiting the URL directly (/?state=1) you would have no data in the state, data will only be available when navigating back/forward while pushing state via History.pushState.

side note: Make sure your navigationInProgress variable is fixed, you don't want it to be stalled there. Reset it when the $.get request failed when listening on the error callback. And when your pushNewUrl is false, reset the navigationInProgress attribute.

like image 102
Mohamed Mansour Avatar answered Oct 04 '22 13:10

Mohamed Mansour