What exactly is the pushState state object in HTML?


Use the pushSate object to update the page when the user navigates back through history. Let us see an example to include the selected color that creates a history entry −

function display(color) {
   var myState = { selectedColor: color },
   myTitle = "Page title",
   myPath = "/" + color;
   history.pushState(myState, myTitle, myPath );
};

Now we will use the popstate event to update the selected color −

$(window).on('popstate', function(event) {
   var myState = event.originalEvent.state;
   if (statemyState {
      selectColor( myState.selectedColor );
   }
});

Updated on: 25-Jun-2020

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements