0
I need to "replace" the function of the browser back button, so that when the user clicks it is redirected to a page. I’m already using a snippet I found but its operation is not very consistent, and if the user has not clicked at least once on the screen, it does not work.
<script>
(function(window, location) {
history.replaceState(null, document.title, location.pathname+"#!/history");
history.pushState(null, document.title, "");
window.addEventListener("popstate", function() {
if(location.hash === "#!/history") {
setTimeout(function(){
document.getElementById('breadCurso').submit();
},0);
}
}, false);
}(window, location));
</script>
I am redirecting through a form ('breadCurso' ID), so that the links are protected by the POST, and "including" the pages through PHP.
Encapsulate the function correctly and try again (Function() { ... })(window, Location)
– ruansenadev
@ruansenadev I keep having the same problem of having to click on the screen at least once before. If I don’t click, I’m redirected to the main page.
– Richard Lucas
This is not very interesting, tends to make the user angry of the site, there is no way to prevent him from using the button, just as there is no way to block the console, more you can change the history, so if he press back, would go to another page, more is not good, mainly.
– Macedo_Montalvão