Hey,
I've tried many back button scripts available on STM and even outside (on StackOverflow and other websites) but none of them have worked. Does someone have any back button script which actually works on Chrome on Android?
This one definitely works:
<script type="text/javascript">
(function(window, location) {
history.replaceState(null, document.title, location.pathname+"#!/backbutton");
history.pushState(null, document.title, location.pathname);
window.addEventListener("popstate", function() {
if(location.hash === "#!/backbutton") {
history.replaceState(null, document.title, location.pathname);
setTimeout(function(){
location.replace("http://your.tracking.link/");
},0);
}
}, false);
}(window, location));
</script>
Thanks, that certainly works
Additionally, what do I need to do if I want to not allow the user to go anywhere at all after pressing the back button, I mean I want him to stay on the same exact page, I tried replacing the tracking link by # but that only keeps the user on the current page for one back button press, if the user presses the back button twice, he goes out. Is there a way to keep the user stuck on the page?
Besides this, do you have a way to make alert/beep sound work when the page load? I tried using the audio tag with autoplay but it doesn't work on Chrome.
I found a snippet which allows to keep the user on the same page even after multiple back button presses:
<script type="text/javascript">
history.pushState({ page: 1 }, "title 1", "#nbb");
window.onhashchange = function (event) {window.location.hash = "nbb";};
</script>