How popstate works based on browser back button

I am confused on the working fuction of popstate.

Example.com (Clicked href) -> Google.com (Pressed Back) -> Yahoo.com

(OR)

Example.com (Clicked href) -> Google.com (Pressed Back) -> Example.com
(Quick redirect automatically) -> Yahoo.com

My code

// Listen for the popstate event
window.addEventListener('popstate', function(event) {
  // Redirect the user to Yahoo.com
  window.location.href = 'https://www.yahoo.com';
});

// When a link is clicked, update the URL to include the current URL as a parameter
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
  links[i].addEventListener('click', function(event) {
    var currentUrl = encodeURIComponent(window.location.href);
    var href = this.getAttribute('href');
    this.setAttribute('href', href + '?prev=' + currentUrl);
  });
}