Blocking the Back Button

Question: Can we block the back button on the browser so that if we click on back button it is ignored?
Ravi Kumar

Answer: For security reasons client side processing is restricted to accessing only the content of the web page and cookies set by the site. The back button is part of the browser and is therefore not allowed to be messed with. There may be one or more browsers with an unplugged security hole that allows such access but any sensible person would not run a browser that allowed that level of access to their computer.

While you can't block the back button directly from JavaScript, there are a few things that you can do that will affect what the back button does. Recently visited web pages are stored in history and the back button loads the page that is in the previous location in this history. JavaScript cannot see what is stored in the history but it can manipulate the history to a small extent.

There are basically two manipulations that JavaScript can perform with the history. The first is that it can load any page in history over the current page by calling history.go() passing it a number corresponding to how far forward in the history that the page is (so the page the back button would access is -1 in the history). The second thing that JavaScript can do is to open a new page and decide whether to add that page to the history or to overwrite the current page in the history with the new one. If you do the second of these then the back button will not go back to the page that loaded the current page but to the page before that as the immediately preceding page is no longer in the history. Neither of these changes will prevent your visitors using the back button.

 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate