Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pushState: what exactly is the state object for?

I've read a dozen of times now that the state object could exists of multiple key|value pairs and that it is associated with the new history entry. But could someone please give me an example of the benefits of the state object? Whats the practical use of it? I can't imagine why not just typing in {}

like image 287
campari Avatar asked Jul 12 '13 09:07

campari


People also ask

What is pushState state?

state. The state object is a JavaScript object which is associated with the new history entry created by pushState() . Whenever the user navigates to the new state , a popstate event is fired, and the state property of the event contains a copy of the history entry's state object.

What does the pushState () method do?

API Overview The pushState() method enables mapping of a state object to a URL. The address bar is updated to match the specified URL without actually loading the page.

What is the difference between pushState and replaceState?

replaceState() operates exactly like history. pushState() except that replaceState() modifies the current history entry instead of creating a new one. replaceState() is particularly useful when you want to update the state object or URL of the current history entry in response to some user action.

Does pushState trigger Popstate?

pushState() or history. replaceState() won't trigger a popstate event. The popstate event will be triggered by doing a browser action such as a click on the back or forward button (or calling history. back() or history.


2 Answers

Take this small example - run fiddle:

You have a page where a user can select a color. Every time they do, we generate a new history entry:

function doPushState (color) {     var state = {},         title = "Page title",         path  = "/" + color;          history.pushState(state, title, path); }; 

We leave the state object blank for now and set the URL to the color name (don't reload the page - that URL doesn't exist, so you will get a 404).

Now click on a red, green and blue once each. Note that the URL changes. Now what happens if you click the back button?

The browser does indeed go back in history, but our page doesn't notice that - the URL changes from '/blue' back to '/green', but our page stays at 'You have selected blue'. Our page has gone out of sync with the URL.

This is what the window.onpopstate event and the state object are for:

  1. We include our selected color in our state object
function doPushState (color) {     var state = { selectedColor: color }, // <--- here         title = "Page title",         path  = "/" + color;          history.pushState(state, title, path); }; 
  1. Then we listen for the popstate event, so that we know when we have to update the selected color, which is this:
window.addEventListener('popstate', function (event) {     var state = event.state;          if (state) {         selectColor( state.selectedColor );     } }); 

Try the updated example: run fiddle: our page now updates accordingly when the user navigates back through history.

like image 130
janfoeh Avatar answered Sep 21 '22 12:09

janfoeh


Is a specific and forward looking use case the maintenance of user view and data state in a progressive app using custom elements and templates that are divided up in the view regionally

Imagine a 64 box grid as your view, on a large screen the boxes are 147 ^2 a piece

The url represents 64/ a user ID abs related user data

The web app can then fill its grid with user specific state data

In this use case, one I fully believe is the future, the user wouldn't want to share his or her personal state and data filled view portions

By using previous history states and their related 650k of data

A whole, complex app can be, reassembled from browser history and location, including state, using a few well known sort approaches.

It's cool

like image 34
Jason Frazzano Avatar answered Sep 23 '22 12:09

Jason Frazzano