title: How to enable history back button when using location.href = "site"?
body: <p>I am new to js and html. I have a js code to redirect to a webpage in the same site, from <a href="https://example.com/html1.html" rel="nofollow noreferrer">https://example.com/html1.html</a> to <a href="https://example.com/html2.html" rel="nofollow noreferrer">https://example.com/html2.html</a>. I also want it so the back button can be enabled, but from what I have found from searching nothing provides an answer for what I am looking for.</p>
  <p>Here is code:</p>
  <pre class="lang-js prettyprint-override"><code>location.href = &quot;https://example.com/html2.html&quot;;
  </code></pre>
  <p>For some reason the back button to go to the previous page (<a href="https://example.com/html1.html" rel="nofollow noreferrer">https://example.com/html1.html</a>) does not work.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>location.href = "https://stackoverflow.com/";
  //Firebase authstate changed listener here
  //Timeout represents when the user signs in
  setTimeout(() =&gt; {location.href = "https://example.com/";}, 500);</code></pre>
  </div>
  </div>
  </p>
  <p>I have a hotfix which doesn't always work but it just puts a 1 sec delay and it works</p>
  <pre class="lang-js prettyprint-override"><code>firebase.auth().onAuthStateChanged(user =&gt; {
      if (user) {
          console.log(&quot;User: &quot; + user.email + &quot; Displayname: &quot; + user.displayName);
          console.log(&quot;Url: &quot; + location.href);
          console.log(location.href == &quot;site1&quot;); //Logging if we are in the site1 or not
          if (location.href == &quot;site1&quot;) {
              setTimeout(() =&gt; {
                  location.href = &quot;site2&quot;;
              }, 1000);
              console.log(&quot;Not in site2&quot;);
          } else {
              console.log(&quot;In site2&quot;);
          }
          console.log(&quot;Going to main blog&quot;);
      }
  
  });
  </code></pre>