Tuesday, April 12, 2011

Disable Back Button and Right Click using JavaScript

Categories:

Now a day due to security purpose sometimes it is required that we disable the functionality of Back button of the Browser. Actually we can not disable Browser Back button but we can use JavaScript and find a workaround for this. We will cover below 3 tricks in this Article one by one -
  1. Open a Window without Back Button.
  2. Disable Back functionality using history.forward().
  3. Disable the right click on any webpage using JavaScript.
Open a window without Back Button
Although this is one of the ways to hide Back button from user but at the same time there is an Alternate workaround for the user to go back the previous page. Most of the modern browsers have options of Back in context menu. Hence even if you disable Back Button, user can still right click on the page and click Back to go to previous page.(You can disable right click also as described below in this Article)
The codes to open a webpage in a new window have no toolbar (Back/Next buttons) as follows-

<HTML>
<HEAD>
<SCRIPT type="text/javascript">
    
    function openWindow() {
        //Open a new window with no toolbar
        window.open("http://emuzix.net","mywindow","status=1, toolbar=0");

    }
</SCRIPT>
</HEAD>
<BODY>
<input type="button" value="Demo Here" onClick="openWindow()"/> 

</BODY>
</HTML>



Disable Back functionality using history.forward()
Another technique to disable the back functionality in any webpage is using history.forward() method. We need to add the following codes in the webpage where we want to avoid user to get back from previous page. But here the problem 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 add following code in page1

<HTML>
<HEAD>
<SCRIPT type="text/javascript">

    function stayHere() {
        window.history.forward();
    }
</SCRIPT>
</HEAD>
<BODY onload="stayHere();">

Your Content Goes Here!

</BODY>
</HTML>



Disable the right click on any webpage using JavaScript
Basically "oncontextmenu" is responsible for opening contextmenu in a page. Add the below code to Disable the right click (Context menu) on any webpage –

<HTML>
<BODY oncontextmenu="return false;">

Your Content Goes Here!

</BODY>
</HTML>

Spread The Love, Share Our Article

Related Posts

3 Response to Disable Back Button and Right Click using JavaScript

November 25, 2011 at 10:53 PM

how to use this in blogspot



my blog is

http://howtohackthe.blogspot.com

Anonymous
May 20, 2012 at 11:33 PM

You can use CSS only for this selection disable operation... Please check this example.. http://aspnettutorialonline.blogspot.com/2012/04/how-to-disable-text-selection-on-web.html

Anonymous
October 8, 2013 at 9:40 PM

This is really attention-grabbing, You are an excessively professional blogger.I have joined your rss feed and sit up for in quest of more of your great post.
website design

Post a Comment