Handling window.onpopstate events

The window.onpopstate event is fired automatically by the browser when a user navigates between history states that a developer has set. This event is important to handle when you push to history object and then later retrieve information whenever the user presses the back/forward button of the browser.

Here's how we'll program a simple popstate event:

window.addEventListener('popstate', e => {    console.log(e.state); // state data of history (remember history.state ?)})

Now we'll discuss some methods associated with the history object.

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.