I am trying to develop a javascript code that when a user clicks on the back button of a webpage, it can be redirected to another URL.
I have found these lines of co
I have found the answer in case someone else is looking for it.
Create a separate file called history-stealer.js with the following code:
(function(window, location) {
history.replaceState(null, document.title, location.pathname+"#!/history");
history.pushState(null, document.title, location.pathname);
window.addEventListener("popstate", function() {
if(location.hash === "#!/history") {
history.replaceState(null, document.title, location.pathname);
setTimeout(function(){
location.replace("http://www.url.com");
},10);
}
}, false);
}(window, location));
and then include this code in your HTML file:
so that it calls the history-stealer.js file