View video tutorial

HTML onpopstate Attribute

HTML

The HTML onpopstate attribute is an event handler used to specify a script to run when the browser's session history changes.

Definition and Usage


➔ This is mainly related to the window object or <body> element.

➔ This usually happens when a user navigates through the browser history using the back or forward buttons or JavaScript history object methods.

Syntax
//In HTML
<body onpopstate="myFunction()">
//In javascript
window.addEventListener('popstate', (event) => {
  console.log(event.state); 
});

Applies to

This attribute can be used on the following element.

Attribute Element
onpopstate <body>, window