As current (not previous) state on popstate windows

I grab popstate and have to do various things based on the current and originalEvent on the history stack. However, I cannot figure out how to get the current state for comparison, but originalEvent works. Here is my JS code. The first IF statement throws an exception that e.state.id is undefined ...

$(window).on("popstate", function(e) {
        //check and compare current state
        if (e.state.id == 'something') {  // throws an exception
            *do something*
        } else if (e.originalEvent.state !== null) { //previous state exists?
            *do something*
        } else { //no previous state
            *do something*
        }
    });

      

FYI, the pressed state is set as:

history.pushState({id: 'view'}, '', '/view');

      

+3


source to share





All Articles