Click here to Skip to main content
14,921,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Can any one help to disable browser back,forward and back button and right Click Menu functionality using javascript or Jquery?I have tried disabling the back button like this
JavaScript
function disableBackButton() {
           window.history.forward();
       }
       setTimeout("disableBackButton()", 0);


I have called this function in the Body onload event.I have a doubt that where we need to put this code in the Calling page or Called Page.Suppose i have two pages like this FirstPage.aspx and SecondPage.aspx.When i navigate from the First Page to Second Page when i click back button of the browser it should not go to FirstPage.aspx.

Please help me regarding this
Posted

Don't do this, you shouldn't be tring to mess with browser functionality. The browser is mine, I want my back \ forward and refresh buttons to work!!

Usually when people post questions like this, it's because they don't understand how to control the flow of data from the site, due to things like caching etc.

Why shouldn't it go back to Firstpage? That's the 'expected behaviour' of how the internet works. Is it because something has been filled in, some security issue? Depending on what it is you're trying to stop the user doing (other than going back!) there's probably another way to achieve this.
   
Comments
hemantwithu 22-Aug-11 4:42am
   
Hi Dylan,But i have such requriment in my project.Please suggest me some idea.
Dylan Morley 22-Aug-11 4:46am
   
Why? What are you trying to achieve. Why shouldn't the user go back?

Basically, there's no way to uniformly disable buttons across all browsers. IE, Firefox, Chrome all have different functionality. What if I disable javascript (my browser has script disabled for all sites until I trust them) - your site isn't going to work.

You don't control the client, so you cannot determine exactly what's going to happen here. You only control the server side code, so this is where you should design your site behaviour.
hemantwithu 22-Aug-11 4:51am
   
Ok.How can i do this from Server Side?Like Clearing the Cache Memory.
Dylan Morley 22-Aug-11 5:00am
   
Have a read here.

http://www.codeguru.com/csharp/.net/net_debugging/debugging/article.php/c12891

Again, it depends what you're trying to achieve. If the user clicks back and you don't want them to go to the previous page, where should they go? To the home page? To a login page? What should happen?

In the article above, you could change the bit where it does a Response.Redirect("expired.htm"); to whatever page you want to send the user to.
hemantwithu 22-Aug-11 5:07am
   
My Achievement is to make the user not to go back and stay in the Same Page.
Smrng 20-Jul-12 7:35am
   
I have created a login page.
The person when logins and hit the back button,he goes to the main page.Thats okay but the problem is when he hits the forward button,he enters the page without even logging in.What can be done?
Dylan Morley 20-Jul-12 7:43am
   
Have a read of this,

http://www.codeproject.com/Articles/347761/Web-Development-Disabling-the-back-button

So, add authentication to your page that you don't want the user to be able to move into. Make sure you set the caching attributes so the page can't be cached. When the user moves forward, they will be redirected to the login page.
yes we certainly can not disable browse buttons cause its not ours its browser specific but we can disable certain function keys like F5 to refresh browser and F1 for Help menu in IE explorer, here is the code spinet that will disable browser F5 button to refresh browser contents.


HTML
document.onkeydown = function() {
    if(event.keyCode == 116) {
            event.returnValue = false;
            event.keyCode = 0;
            return false;
           }
}


place this code anywhere in JavaScript and your keyboard short cut key for F5 will be disabled for that particular page. 116 is a numeric code for F5. You can disable any button by finding numeric code for it
   
v2
window.history.forward(1);
document.attachEvent("onkeydown", my_onkeydown_handler);
function my_onkeydown_handler()
{
switch (event.keyCode)
{
case 116 : // 'F5'
event.returnValue = false;
event.keyCode = 0;
//window.status = "We have disabled F5";
break;
}
}
document.onmousedown=disableclick;
status="Right Click is not allowed";
function disableclick(e)
{
if(event.button==2)
{
alert(status);
return false;
}
}

heres the code to disable the right click and F5 key in your browser
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900