cover-img

popstate

16 March, 2023

0

0

0

About

The popstate event fires whenever the active history entry changes due to user navigation, such as clicking the back or forward buttons in the browser. The event is triggered on the window object and allows developers to handle changes to the browser's history stack and update the page accordingly.

The popstate event is often used in conjunction with the HTML5 History API, which provides a way for developers to manipulate the browser's history stack directly. With the History API, developers can add or remove entries from the history stack, change the URL displayed in the address bar, and navigate to specific entries in the history stack. The popstate event is used to handle these changes and update the page content accordingly, without requiring a full page reload. This allows developers to create single-page applications that offer a smoother user experience by providing dynamic content updates without interrupting the user's browsing flow.

Event listener

Here's an example of how you can use the addEventListener method to listen for the popstate event:

HTML

<p>Click the links below to change the page content and history stack:</p>
<ul>
<li><a href="/page1">Page 1</a></li>
<li><a href="/page2">Page 2</a></li>
<li><a href="/page3">Page 3</a></li>
</ul>
<div id="content"></div>

JavaScript

// Listen for the popstate event and update the page content.
window.addEventListener('popstate', function(event) {
// The event object contains information about the history state.
// You can use this information to update the page content.
const url = event.target.location.pathname;
updatePageContent(url);
});

// Listen for click events on the links and navigate to the corresponding pages.
document.querySelectorAll('a').forEach(link => {
link.addEventListener('click', function(event) {
event.preventDefault();
const url = this.getAttribute('href');
navigateToPage(url);
});
});

// Define a function that updates the page content.
function updatePageContent(url) {
// Fetch data from the server and update the page content.
}

// Define a function that updates the history stack and calls the updatePageContent function.
function navigateToPage(url) {
// Update the history stack and call the updatePageContent function.
history.pushState({}, '', url);
updatePageContent(url);
}

Here, when the user clicks on one of the links, the navigateToPage function is called, which updates the history stack and calls the updatePageContent function to fetch data from the server and update the page content. When the user clicks the back or forward button, the popstate event is fired and the updatePageContent function is called again with the new URL.

Property

Here's an example of how you can use the onpopstate property to listen for the popstate event:

// Listen for the popstate event using the onpopstate property.
window.onpopstate = handlePopState;

// Define a function that updates the history stack and calls the updatePageContent function.
function navigateToPage(url) {
// Update the history stack and call the updatePageContent function.
history.pushState({}, '', url);
updatePageContent(url);
}

// Listen for click events on the links and navigate to the corresponding pages.
document.querySelectorAll('a').forEach(link => {
link.addEventListener('click', function(event) {
event.preventDefault();
const url = this.getAttribute('href');
navigateToPage(url);
});
});

// Define a function that updates the page content.
function updatePageContent(url) {
// Fetch data from the server and update the page content.
// This function is specific to your application and is not shown here.
}

// Define the function that will handle the popstate event.
function handlePopState(event) {
// The event object contains information about the history state.
// You can use this information to update the page content.
const url = event.target.location.pathname;
updatePageContent(url);
}

Here, we define a function handlePopState that will handle the popstate event. We then set the onpopstate property of the window object to the handlePopState function, which will be called whenever the popstate event is fired. The rest of the code is similar to the previous example, where we define functions to update the page content and navigate to different pages when the user clicks on the links.

Inline

Here's an example of how you can use onpopstate inline in HTML:

<p>Click the links below to change the page content and history stack:</p>
<ul>
<li><a href="/page1" onpopstate="updatePageContent(this.getAttribute('href'))">Page 1</a></li>
<li><a href="/page2" onpopstate="updatePageContent(this.getAttribute('href'))">Page 2</a></li>
<li><a href="/page3" onpopstate="updatePageContent(this.getAttribute('href'))">Page 3</a></li>
</ul>
<div id="content"></div>

Here, we set the onpopstate attribute of each link to a function call that updates the page content based on the URL of the link. When the user clicks on a link, the pushState method is called to update the history stack, and the updatePageContent function is called to update the page content.

While using onpopstate inline in HTML is possible, it can lead to code duplication and makes it harder to maintain the code as it grows. It is generally better to keep your JavaScript code separate in a separate file as shown in the previous examples.

Programmatic trigger

There is a popstate() method which enables you to trigger the popstate event programmatically:

// Define a function to handle the popstate event.
function handlePopState(event) {
console.log('Popstate event fired');
}

// Listen for the popstate event using the addEventListener method.
window.addEventListener('popstate', handlePopState);

// Call the popstate method to trigger the popstate event programmatically.
window.history.popstate();

Here, we define a function handlePopState to handle the popstate event. We then use the addEventListener method to listen for the popstate event. Finally, we call the popstate() method on the history object to trigger the popstate event programmatically.

Note that when you use the popstate() method to trigger the popstate event, the state object passed to the pushState() or replaceState() method when the state was added to the history stack is not passed along with the event. Therefore, you should ensure that your code can handle this scenario appropriately.

javascript

0

0

0

javascript

Milsaware
C#, PHP, Javascript, Kotlin App Developer

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2025. Showcase Creators Inc. All rights reserved.