title: Browser Back Button Redirect Script
body: <p>I am trying to develop the Browser Back Button redirect script.</p>
  <p>So basically when the user arrives at index.html for google or any other social media, on clicking the back button the user should go to the specified page.</p>
  <p>I tried creating it with javascript, But it's not working. Anybody can help about it?</p>
  <p><strong>I created 3 pages</strong></p>
  <p><strong>1) Index.html</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body&gt;
  
  &lt;p&gt;&lt;a href=&quot;redirect.html&quot;&gt;Redirect Page&lt;/a&gt;&lt;/p&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>2)redirect.html</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;script&gt;
  function init() {
  setTimeout(function(){window.scrollTo(0,1)},0);
  }
  
  window.history.pushState(‘back.html’, ‘back’, ‘back.html’);
  window.history.pushState(‘index.html’, ‘Index’, ‘index.html’);
  window.addEventListener(“popstate”, function(e) {
  if(document.URL.indexOf(“back.html”) &gt;= 0){
  document.location.href = document.location;
  }
  
  });
  &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;p&gt;By pressing the browser back button it should redirect to specified url&lt;/p&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>3) back.html</strong></p>
  <pre><code>&lt;html xmlns=”http://www.w3.org/1999/xhtml”&gt;
  &lt;head&gt;
  &lt;script&gt;
  
  document.location.href = “https://www.google.com”;
  
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>