2

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?

1

3 Answers 3

9

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

Sign up to request clarification or add additional context in comments.

3 Comments

Can someone explain why the first 2 lines are required? Just adding window.onpopstate = () => history.forward(); is working fine for me
where do I add this line of code? I am using ASP.Net MVC 4.8
@Saksham Chaudhary infosec security issues will come with : window.location.href
2

You can't disable the browser's back button. If you could, that would be a security hazard and the browser vendors would most likely seek to fix it.

2 Comments

according to the acceped answer this is deprecated, right?
no. the first answer is manipulating history, not disabling the back button
0

The HTML5 History API gives developers the ability to modify a website's URL without a full page refresh. This is particularly useful for loading portions of a page with JavaScript, such that the content is significantly different and warrants a new URL.

You can check this link it may helpful

https://css-tricks.com/using-the-html5-history-api/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.