How to stop users to go back to the previous pages upon pressing browsers back button by using javascript. Any suggestions greately appreciated.**
Thanks and Regards
Bannister
How to stop users to go back to the previous pages upon pressing browsers back button by using javascript. Any suggestions greately appreciated.**
Thanks and Regards
Bannister
Thankfully, it's impossible. You cannot stop the user's back button from working, but you could technically change the url enough times where it becomes pointless to even try using the back button to navigate.
// treat the URL changes as a non-external page (the page won't reload)
document.location.href += "?";
// Fill up the browser's history
for(var i = 0; i < 100; i++){
document.location.href += "a";
document.location.href = document.location.href.substring(0, document.location.href.length-1)
}
This would fill the browser's history with the same page, effectively disabling the back button for any practical use.
Use this on page load, it will stop user on navigating back from page on which it placed.
<script type = "text/javascript">
function changeHashOnLoad() {
window.location.href += "#";
setTimeout("changeHashAgain()", "50");
}
function changeHashAgain()
{
window.location.href += "1";
}
var storedHash = window.location.hash;
window.setInterval(function () {
if (window.location.hash != storedHash) {
window.location.hash = storedHash;
}
}, 50);
</script>
why don't you just confirm the user action when pressing the back button?
something like
window.onbeforeunload = function(){
return "Message here";
}
it will prevent user who accidentally click back, forward, or close the window and change the url directly.
By using javascript you can do this.
Refer this,
http://www.htmlgoodies.com/tutorials/buttons/article.php/3478911
This may helps you
My solution without any confirmations and useless history fill:
Add one dummy history instance when load. if click on btn, add another.
if (window.history && history.pushState) {
addEventListener('load', function() {
history.pushState(null, null, null);
addEventListener('popstate', function() {
history.pushState(null, null, null);
});
});
}