title: Is it possible to capture the window.location.replace event?
body: <p>If I'm currently at the URL<code>"example.com/somepage#somehash"</code> and I invoke <code>window.location.hash = "anotherhash"</code>, the URL changes to <code>"example.com/somepage#anotherhash"</code>. This fires the <code>window.hashashchange</code> event.</p>
  
  <p>If I am currently at the URL <code>"example.com/somepage?a=1&amp;b=two"</code> and I invoke <code>window.location.replace("?one=1&amp;two=2")</code>, then the URL changes to <code>"example.com/somepage?one=1&amp;two=2"</code>. </p>
  
  <p>I've read the <a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onhashchange" rel="noreferrer">MDN docs</a>, and I can't find an even that this fires.</p>
  
  <ol>
  <li>Is there one?</li>
  <li>If not, is there a simple way to capture that event?</li>
  </ol>
  
  <h2>Note:</h2>
  
  <p>It's my fault for saying that I want to make sure I don't trigger a page reload. I want to use the new URL for updating the page based on the query string, for example with an AJAX request. <code>window.history.pushState</code> is another option, but as far as I can tell, it does not fire an event either.</p>
  
  <h2>Edit</h2>
  
  <p>I took a look at @Taki's answer. I create a <a href="https://repl.it/@abalter/windowlocationreplace-event" rel="noreferrer">repl.it</a> because you can see the URL change when you go to the full-page view. However, even with the <code>preventDefault</code> the page is reloading, which is proved by the fact that the info posted to the page in the unload event callback disappears. Consequently, this can't be used for client-side routing, which is my goal.</p>
  
  <p><em>index.html</em></p>
  
  <pre><code>&lt;button id="myBtn"&gt;Click me&lt;/button&gt;
  &lt;div id="info"&gt;&lt;/div&gt;
  &lt;script src="https://code.jquery.com/jquery-3.3.1.js"&gt;&lt;/script&gt;
  &lt;script src="index.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p><em>index.js</em></p>
  
  <pre><code>console.log("index.js");
  
  $('#myBtn').on('click', function(e)
  {
    console.log("button clicked")
    window.location.replace("?one=1&amp;two=2")
    console.log(window.location.href);
  });
  
  $(window).on("beforeunload", function (event) 
  {
    event.preventDefault(); // just to pause and see the cosdole
    console.log("beforeunload");
    console.log(event);
    $('#info').append("&lt;p&gt;beforeunload&lt;/p&gt;");
    console.log(window.location.href); // capture the url
  });
  </code></pre>