In this article, we will discuss how to write a javascript function which will prevent the user to navigate back to the last or previous page. There are so many ways to stop the browser back button most popular and will work in all conditions. You can add code to the first or previous page to force the browser to go forwards again and again so when the user tries to back to the previous page the browser will take him again in the same.
This can be done by making custom functions like this:
Example 1:
- Code 1: Save this file as Login.html for the first page.
<!DOCTYPE html><html><head><title>Blocking Back Buttonusing javascript</title><styletype="text/css">body {font-family:Arial;color:green;}</style><scripttype="text/javascript">window.history.forward();function noBack() {window.history.forward();}</script></head><body><h1>GeeksforGeeks</h2><p>Click here to Goto<ahref="b.html">Link to second page</a></p></body></html>chevron_rightfilter_none - Code 2: Save this file as b.html for the second page.
<!DOCTYPE html><html><head><title>Blocking Back Buttonusing javascript</title></head><body><h3>This is second page</h3><p>On this page, back buttonfunctionality is disabled.</p></body></html>chevron_rightfilter_none - Output:

Example 2:
- Code 1: Save this file as a.html for the first page.
<!DOCTYPE html><html><head><title>First Page</title><scripttype="text/javascript">function preventBack() {window.history.forward();}setTimeout("preventBack()", 0);window.onunload = function () { null };</script></head><body><h3>This is first page</h3><hr/><ahref="b.html">Goto second Page</a></body></html>chevron_rightfilter_none - Code 2: Save this file as b.html for the second page.
<!DOCTYPE html><html><head><title>Second Page</title></head><body><h3>Second Page - Back Buttonis disbaled here.</h3><hr/></body></html>chevron_rightfilter_none - Output:

Recommended Posts:
- How to make browser to go back to previous page using JavaScript ?
- How to redirect browser window back using JavaScript ?
- How to stop submit button from expanding along with textarea?
- How to trigger HTML button after hitting enter button in textbox using JavaScript ?
- How to check a webpage is loaded inside an iframe or into the browser window using JavaScript?
- How to close current tab in a browser window using JavaScript?
- Cross-browser window resize event using JavaScript/jQuery
- How to detect the user browser ( Safari, Chrome, IE, Firefox and Opera ) using JavaScript ?
- How to Display Changed Browser URL Without Reloading Through alert using JavaScript ?
- How to detect the browser language preference using JavaScript ?
- How to stop event propagation with inline onclick attribute in JavaScript ?
- How to create a radio button similar to toggle button using Bootstrap ?
- JavaScript | Detecting a mobile browser
- How to detect browser or tab closing in JavaScript ?
- How to connect SQL Server database from JavaScript in the browser ?
- How to open a PDF files in web browser using PHP?
- How to give a div tag 100% height of the browser window using CSS
- How to jump to top of browser page using jQuery?
- How to get the size of screen, current web page and browser window using jQuery?
- button tag vs input type="button" attribute
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


