title: Update history state and redirect
body: <p>How can I update the history state and then redirect to a new page so that the back button returns to the updated history.</p>
  <p>ie:</p>
  <ul>
  <li>I go to search.html?query=test</li>
  <li>Update history to search.html?waitQuery=test</li>
  <li>Redirect to Google search &quot;test&quot;</li>
  <li>Hit back button, go to search.html?waitQuery=test</li>
  </ul>
  <p>For context: At my work site we use a ticketing system and to speed up my workflow I've created a local script file and set it as a custom search engine in Chrome. It uses regular expressions to check if a query matches a ticket ID pattern and if so will redirect to the ticket system, and if not redirects to Google.</p>
  <p>This way if I search something like &quot;TK00123&quot; in the address bar, it redirects to <code>http://ticketengine/query?TK00123</code> but if I search &quot;hello world&quot; it just forwards that query to Google.</p>
  <p>I would like to be able to hit the back button after the redirect and get a sort of interstitial page where I could choose from some options.</p>
  <p>This is what I have currently:</p>
  <pre><code>&lt;script&gt;
  let ticketSearch = 'http://ticketengine/query?'
  let googleSearch = 'https://www.google.com/search?q='
  if (location.search.match(/\?query=(.+)/)) {
      let searchQuery = RegExp.lastParen
      history.pushState({}, '', location.pathname + '?waitQuery=' + RegExp.lastParen)
      if (searchQuery.match('TK\d+')) {
          location.href = ticketSearch + searchQuery
      } else {
          location.href = googleSearch + searchQuery
      }
  } else if (location.search.match(/\?waitQuery=(.+)/)) {
      let searchQuery = RegExp.lastParen
      document.write(`Ticket: &lt;a href=&quot;${ticketSearch + searchQuery}&quot;&gt;${decodeURIComponent(searchQuery)}&lt;/a&gt;&lt;br&gt;`)
      document.write(`Google: &lt;a href=&quot;${googleSearch + searchQuery}&quot;&gt;${decodeURIComponent(searchQuery)}&lt;/a&gt;&lt;br&gt;`)
  }
  &lt;/script&gt;
  </code></pre>
  <p>From everything I've read this <strong>seems</strong> like it should work, but I never actually get that history entry that I can go back to. The redirect wipes it out.</p>