Run a script after location is changed, Javascript

I want to achieve something like this,
location.replace("some url"); alert("hello");

I know this won’t work but it runs before the next page is loaded and onload doesn’t seem to work either is there a way to achieve this ? Please just don’t tell there isn’t, although this isn’t a necessity it will be great if I can handle that in my project. THANK YOU