Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to disable the browser's back button

I've written two HTML files:

  1. Login.html <a href = "Home.html">Next Page</a>
  2. Home.html`

<html>
   <body>
      <a href = >Login.html>>Prev Page</a>
   </body>
<script type = "text/javascript" >

	history.pushState("anything", "", "#1");
    	window.onhashchange = function (event) {
       		window.location.hash = "a";
    	};
</script>
</html>

` I'm trying to disable browser's back button. If i execute this code on chrome it doesn't disable the back button but if i run history.state command in console of Home.html page and then i click the back button, then it remains on same page(works as expected). Why so?

like image 601
Saksham Chaudhary Avatar asked Dec 14 '25 03:12

Saksham Chaudhary


1 Answers

FOA, Thanks everyone for your answers.

Finally below code gave me the solution:

history.pushState(null, null, window.location.href);
history.back();
window.onpopstate = () => history.forward();
like image 125
Saksham Chaudhary Avatar answered Dec 16 '25 20:12

Saksham Chaudhary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!