title: Javascript: Modern working approach to refresh the page redirects to another location
body: <p>I have tried some answers on SO, but they are old and no longer work, or are blocked by the browser.</p>
  <p>I have a login, and a separate login failed page (separate for a good reason, please don't say make them the same page).</p>
  <p>If the user is shown the failed login page, and they hit refresh, I would like to go to the login page, not refresh the failed login page.</p>
  <p>Existing answers go something like this:</p>
  <pre class="lang-js prettyprint-override"><code>&lt;script type=&quot;module&quot;&gt;
      function redirect()
      {
          window.location.href=&quot;/login/&quot;;
          return true;
      }
      window.addEventListener('beforeunload', redirect);
  &lt;/script&gt;
  </code></pre>