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.


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/",

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">
	function noBack() { window.history.forward(); }
<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.

Get our Articles via Email. Enter your email address.

You may also like...


  1. Pavan says:

    Thanks that worked

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


    You simply need to put :


    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


    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.


  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:


    Pls provide me example code for warning message when user clicks back button in jsp.
    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

  8. Santhosh K S says:

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

  9. vikas kumar says:

    Perfect, it working. Thanks very much. its very useful.

  10. sin says:

    Hey..!!! that worked and solved my one week’s issue…Thanks..good one!! :)

Leave a Reply

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