This answer is already given, but I will try to explain what I understand using pushState
Consider your url is "google.com/gmail"
1.Make currentURL as temp URL, so your browser will show this URL. At this step, there will be one URL in the stack i.e google.com/gmail#!/tempURL
.
history.replaceState(null, document.title, location.pathname+"#!/tempURL");
2.Push the previousURL as new state so now your browser will show previousURL i.e google.com/gmail
.
history.pushState(null, document.title, location.pathname);
Current state of the stack
First element : google.com/gmail
Last element : google.com/gmail#!/tempURL
3.Now add listener to listen event
window.addEventListener("popstate", function() {
if(location.hash === "#!/tempURL") {
history.replaceState(null, document.title, location.pathname);
//replaces first element of last element of stack with google.com/gmail so can be used further
setTimeout(function(){
location.replace("http://www.yahoo.com/");
},0);
}
}, false);