2

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

  • Check http://stackoverflow.com/a/12381610/384155 – Osiris Oct 24 '13 at 05:58
  • Why? Do you want to prevent the user from seeing those pages? Is your site designed in such a way that loading those pages would cause some action to be repeated? – user2357112 Oct 24 '13 at 05:58
  • Check this [Demo](http://viralpatel.net/blogs/demo/javascript-disable-browser-back-button/), read [Blog](http://viralpatel.net/blogs/disable-back-button-browser-javascript/) – super Oct 24 '13 at 06:01
  • possible duplicate of [how to stop browser back button using javascript](http://stackoverflow.com/questions/12381563/how-to-stop-browser-back-button-using-javascript) – Shafeeque Oct 24 '13 at 06:02
  • What did you test so far? A tips is to check out the [about](http://stackoverflow.com/about) page as a new user of this site. :) – Qben Oct 24 '13 at 06:18

5 Answers5

2

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.

James Bruckner
  • 909
  • 7
  • 10
2

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>
Arul Dinesh
  • 550
  • 4
  • 15
1

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.

Bobby Stenly
  • 1,260
  • 3
  • 13
  • 23
0

By using javascript you can do this.

Refer this,

http://www.htmlgoodies.com/tutorials/buttons/article.php/3478911

This may helps you

Vinod VT
  • 6,946
  • 11
  • 51
  • 75
0

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);
        });    
    });
}

Reference: https://stackoverflow.com/a/45844767/13672902

Alex
  • 21
  • 3