If I'm currently at the URL"example.com/somepage#somehash"
and I invoke window.location.hash = "anotherhash"
, the URL changes to "example.com/somepage#anotherhash"
. This fires the window.hashashchange
event.
If I am currently at the URL "example.com/somepage?a=1&b=two"
and I invoke window.location.replace("?one=1&two=2")
, then the URL changes to "example.com/somepage?one=1&two=2"
.
I've read the MDN docs, and I can't find an even that this fires.
- Is there one?
- If not, is there a simple way to capture that event?
Note:
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. window.history.pushState
is another option, but as far as I can tell, it does not fire an event either.
Edit
I took a look at @Taki's answer. I create a repl.it because you can see the URL change when you go to the full-page view. However, even with the preventDefault
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.
index.html
<button id="myBtn">Click me</button>
<div id="info"></div>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="index.js"></script>
index.js
console.log("index.js");
$('#myBtn').on('click', function(e)
{
console.log("button clicked")
window.location.replace("?one=1&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("<p>beforeunload</p>");
console.log(window.location.href); // capture the url
});