[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
This 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 ("https://www.viralpatel.net/",
"mywindow","status=1,toolbar=0");
Code language: JavaScript (javascript)
Also it is possible to disable the right click on any webpage using Javascript. Add following code in the webpage.
<body oncontextmenu="return false;">
Code language: HTML, XML (xml)
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="">
Code language: HTML, XML (xml)
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."; };
Code language: JavaScript (javascript)
Include this in your HTML page to popup a warning message. Check the demo for this example.
ONLINE DEMO
View Comments
This is really cool. I have posted it in DZone. Thanks for your time involved.
Thanks Karthikeyan.
How to delete cookie history on a sign out and not allowing back functionality by using javascript
http://geekswithblogs.net/Frez/articles/back-button-issue-after-logout-in-asp.net.aspx
Please refer above link .
Thanks ,
Sameer
Hi Viral,
Thanks for the above code however, using the window. history.forward() function to disable the back browser works well on IE and Opear, but not on Safari and google chrome. Do you know why this is, and if so, could you supply some javascript code that will work on all browsers?
Many Thanks
Nonye
really good
Hi Viral,
Not working for Google chrome. Can you please give javascript code that will work on all browsers?
Many Thanks
see http://www.boutell.com/newfaq/creating/backbutton.html -use setTimeoutto delay call to forward. -should help for other browsers
Only IE its working not in all browsers
but it is not working for Google chrome
Read the below article :
excellent one :
The code works for IE 7, Chrome 4.1, Firefox 3.6.3
http://www.boutell.com/newfaq/creating/backbutton.html
thanks for this useful article. best regards.