I've written two HTML files:
- Login.html
<a href = "Home.html">Next Page</a>
- 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've written two HTML files:
- Login.html
<a href = "Home.html">Next Page</a>
- 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
mand 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 See how to stop browser back button using javascript and How to disable back button in browser using javascript. – showdev Commented Jan 30, 2020 at 10:16
3 Answers
Reset to default 9FOA, 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();
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.
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./using-the-html5-history-api/