Disable Back Button in Browser using JavaScript

[ad name=”AD_INBETWEEN_POST”] Sometimes we have requirement in developing a website to disable the Back button effect from Browser. This is common in Online Banking Websites and other sites where security is of principal concern. User may hit back and navigate from the page and forget to logout.

Hence sometime it is required that we disable the functionality of Back button. But unfortunately this is not an easy task. There is no direct way of dealing with this problem. We can do few hacks to ensure that user does not get back in the browser.

Following are few tricks that can be used to disable the back button in browser. Please note that we do not literally “disable” the back button, but just nullify its effect is some case and hide it altogether in others.

disable-back-button-browser

Open A New Window without Back Button

browser-back-button-viralpatelThis one is very crude technique. But in some case it works like charm. All you have to do is to open the webpage in a new window. This window doesn’t have back button at all because we have hide the toolbar.
This technique does work in some case but user has still a workaround to navigate to previous page. Most of the browser have options of Back in context menu. Thus user can still right click on the page and click Back to go to previous page. We will shortly see the workaround for this issue also.

Following is the code to open webpage in a new window have no toolbar (Back/Next buttons).

window.open ("http://viralpatel.net/blogs/",
"mywindow","status=1,toolbar=0"); 

Also it is possible to disable the right click on any webpage using Javascript. Add following code in the webpage.

 <body oncontextmenu="return false;">  

Disable Back functionality using history.forward

This is another technique to disable the back functionality in any webpage. We can disable the back navigation by adding following code in the webpage. Now the catch here is that you have to add this code in all the pages where you want to avoid user to get back from previous page. For example user follows the navigation page1 -> page2. And you want to stop user from page2 to go back to page1. In this case all following code in page1.

<SCRIPT type="text/javascript">
	window.history.forward();
	function noBack() { window.history.forward(); }
</SCRIPT>
</HEAD>
<BODY onload="noBack();" 
	onpageshow="if (event.persisted) noBack();" onunload="">

The above code will trigger history.forward event for page1. Thus if user presses Back button on page2, he will be sent to page1. But the history.forward code on page1 pushes the user back to page2. Thus user will not be able to go back from page1.

Warn User if Back is Pressed

You may want to warn user if Back button is pressed. This works in most of the cases. If you have some unsaved form data, you might want to trigger a warning message to user if Back button is pressed.

Following Javascript snippet will add a warning message in case Back button is pressed:

window.onbeforeunload = function() { return "You work will be lost."; };

Include this in your HTML page to popup a warning message.

Check the demo for this example.
ONLINE DEMO

Get our Articles via Email. Enter your email address.

You may also like...

120 Comments

  1. Pavan says:

    Thanks that worked

  2. As far as I can tell, you do not need the

    noBack()

    function.
    You simply need to put :

    window.history.forward();

    on the page to which you would not like a history based navigation (the “Back” button in a browser).
    This works in IE8-11, FireFox, Chrome, Opera (all I tested).

    Also, it is much trickier to use the

    window.onbeforeunload

    event because it happens for many more things (like closing the window, forward button, etc) than simply the browser back button. If you are simply trying to make sure a user does not navigate away, it may work for you, but it by no means the equivalent of trapping the “Back” button.

    Thanks!
    -w

  3. lina lansus says:

    nice tutorial. but does it work in all browsers.

  4. Lawakush Kurmi says:

    i have used this code to block (disable) back press on browser but when im clicking more than one time and pressing esc button (keyboard) then it sends to back page. what ‘s happining there pls suggest me any better solution.

  5. sudip says:

    Its works nicely but if you disable the back button then its look more good.
    Another thing i want to know how can I show the “session is expire” after click the logout In PHP . Plz help

  6. Amit says:

    Hi,

    Pls provide me example code for warning message when user clicks back button in jsp.
    or
    code for window.onbeforeunload that where to use it.

    Thanks in advance

  7. salimmejdoub says:

    hello i use jquery mobile
    and sorry i did not work for me what i should do
    thanks

  8. Santhosh K S says:

    Hi viral,
    Please post ONLINE DEMO code. u r post is very useful.

Leave a Reply

Your email address will not be published. Required fields are marked *