title: Redirect to a URL when browser back button is pressed using JavaScript
body: <p>I've done some research but couldn't find a solution.</p>
  
  <p>I have a landingpage.html. I want to redirect a user to anotherpage.html when he/she presses the back button in the browser.</p>
  
  <p>So I've tried this code:</p>
  
  <pre><code>let currentUrl = location.href;
  history.replaceState('', '', 'anotherpage.html');
  history.pushState('', '', currentUrl);
  </code></pre>
  
  <p>It works not quite as expected: when the back button is pressed the browser address bar displays anotherpage.html page, however no actual redirection happens.</p>