Gets the state of the onpopstate event.

110 0

The code is as follows:

$(function() {
 $('a').bind('click', function() {
 var actionObj = $(this);
 var actionText = actionObj.text();
 var stateObj = {
 title : actionText,
 url : actionObj.attr('href'), 
 msg : actionText
 };
 window.history.pushState(stateObj, actionObj, actionObj.attr('href'));
 });
 window.onpopstate = function(e) {
 console.log(e.state);
 };
})

Check out some of the information, and don't know where the problem is.

Chrome or firefox can't. Don't know where to do it..

2 Answers

78 3

In fact, you just need to understand that the is called after the history.back(). So this time e.state returns the state of back in fact, such as I'm in the following code. It took two state at once, and when I clicked the {page: 2} state, 's was pop out, and there was only the {page: 1} in the e.state.

(function() {
 $('a').bind('click', function() {
 window.history.pushState({page : 1}, 'test', '#test1');
 window.history.pushState({page : 2}, 'test', '#test2');
 return false;
 });
 window.onpopstate = function(e) {
 console.log(e.state);
 };
})();
131 0

In fact, you just need to understand that the is called after the history.back(). So this time e.state returns the state of back in fact, such as I'm in the following code. It took two state at once, and when I clicked the {page: 2} state, 's was pop out, and there was only the {page: 1} in the e.state.

(function() {
 $('a').bind('click', function() {
 window.history.pushState({page : 1}, 'test', '#test1');
 window.history.pushState({page : 2}, 'test', '#test2');
 return false;
 });
 window.onpopstate = function(e) {
 console.log(e.state);
 };
})();
83 5

If you want to get the status of the pop, you can first replaceState, and then pushstate.

 window.history.replaceState({page : 1}, '', ''); 
 window.history.pushState({}, '', '#test1');

The popstate event is triggered when the event is triggered. The value of state is % 7bpage, 1 %:

100 1

If you want to get the status of the pop, you can first replaceState, and then pushstate.

 window.history.replaceState({page : 1}, '', ''); 
 window.history.pushState({}, '', '#test1');

The popstate event is triggered when the event is triggered. The value of state is % 7bpage, 1 %:

...