Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Javascript
I used the below code in master page
<script type="text/javascript">
        function noBack()
         {
             window.history.forward()
         }
        noBack();
        window.onload = noBack;
        window.onpageshow = function(evt) { if (evt.persisted) noBack() }
        window.onunload = function() { void (0) }
    </script>
It is working good for me, but still I have a problem.
As I have set the logout operation in a master page, the browser back button is disabled for every page associated with the master page.It is because,code is executed for every page associated with the master page.
I need to Disable Browser Back Button functionality only when logout is clicked.
Posted 23-Jan-11 20:44pm
Edited 23-Jan-11 20:51pm
m@dhu29.5K
v2
Comments
gpaantony at 15-Nov-11 5:15am
   
i too have the same problem..
Member 9379643 at 17-Sep-12 0:38am
   
Its working good for me yaar...Thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

Instead of disabling back functionality you should check user's authentication on your application's pages
(i.e check whether the user has logged in from login page and user has proper keys or not) if he doesn't that he should be redirected to the login page.
You could do this by applying proper authentication on your application.
 
for more information go here
http://msdn.microsoft.com/en-us/library/532aee0e(VS.71).aspx[^]
  Permalink  
v2
Comments
Kosimek at 25-Apr-14 14:34pm
   
Not a solution. The problem is not caused by clicking a link on an active page, the problem is the cached pages. These pages will not check to see if a user has logged since they are served from the client side.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
 
Your code is not all browser supported. Check it for all browser. You have to handle this thing into your code. So don't wast your time in browser back button.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

<script type="text/javascript">
function back_block()
{
window.history.foward(1)
}
</script>
 
call it on body onload event
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

srikanthvuppula wrote: I need to Disable Browser Back Button functionality only when logout is clicked.
 
No you don't. You need to rethink your application logic so that it doesn't matter if the user presses the back button.
 
You know whether or not a person is logged in when viewing a page, so why not simply display a message "you must be logged in to view this page", or display generic, nonm-user-specific information, or at worst force a redirect if they view a page while not logged in.
 
Don't try and mess with the browser. It's either going to only work sporadically, and then, only for those with javascript enabled, and is subject to the whims of those who write the browsers.
  Permalink  
Comments
Sandeep Mewara at 24-Jan-11 6:56am
   
For doing any of these: 'You know whether or not a person is logged in when viewing a page, so why not simply display a message "you must be logged in to view this page", or display generic, nonm-user-specific information, or at worst force a redirect if they view a page while not logged in.'
One has to run certain code on server side. During browser's back button case, server code is not executed and hence we don't have any control on it. Thus, in such scenario, I guess we are left with playing around with History.
 
If I am missing something please share.
Chris Maunder at 24-Jan-11 7:00am
   
Why are you trying to stop someone hitting the back button? I'm assuming it's to stop them viewing a page that should only be viewed when logged in.
 
So, my comment relates to what to do if someone tries to view a page they aren't meant to, and the answer is, handle this on the server side when they try and view the page.
 
If you are trying to stop them, at the client side, from viewing a page you can't. If javascript is disabled then you scripting efforts are a waste. If they manually look up their history and go to the page, or have bookmarked a page and then go to the bookmark, then you're back to square 1.
 
Discourage someone from viewing a page, yes, but don't mess with the history, and don't assume they won't get to the page.
EdMan196 at 19-May-11 16:15pm
   
Good answer, my 4 :) Please see my answer :)
GeekBond at 2-Nov-12 12:12pm
   
I like the login in your answer. +4
Kosimek at 25-Apr-14 14:56pm
   
@Chris Bad answer. This issue has zero to do with rethinking application logic. Pages from the Back button are served from the client side and do not involve the server side where the application logic is located. <br><br>
I have been struggling with this issue for years, have tried every "solution" I have come across and most do not work at all, a few only work on some browsers. There is certainly no published, working, javascript independent, platform independent, cross-browser solution for this issue or I would have found it.<br><br>
The question posed clearly states that Back button functionality should be prevented AFTER logout. Even not caching the page is useless on most browsers since rapid clicking of the Back button will get you past most obstacles. Any javascript solution that does work can be subverted in any case by turning off javascript.<br><br>
In my apps you can still see previously viewed pages even though clicking on any link in them sends you directly back to the login page. However, these viewed pages may contain personal/sensitive information that should not be accessible to anyone just by clicking the Back button.<br><br>
It is of course possible. All banking applications and Hotmail to name some, send you back to the login page when you click the Back button, even when using rapid clicking of the Back button. How this is done is apparently a well-kept secret.<br><br>
If anyone has a REAL cross browser solution for this, I am all ears! And please don't tell me not to mess with the Back button. It's being done all the time. The question is how?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

check this[^] trick
  Permalink  
Comments
EdMan196 at 19-May-11 16:17pm
   
Good answer, my 5 :) Basically what I said though you probably should have said what 'this' was in your answer, because you didn't, I posted my answer assuming this question hadn't been answered properly :P
Dalek Dave at 19-May-11 19:13pm
   
Nice!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

I have always found disabling browser caching solves this. It means the browser HAS to recall a page from your server, even if it is through back button, thus allowing you to handle things properly. Try using:
 
Response.ExpiresAbsolute = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0));
Response.Expires = 0;
Response.CacheControl = "no-cache";
 
in the Page_Load event of your C# code server side.
  Permalink  
Comments
BobJanova at 20-May-11 4:58am
   
Indeed. The problem here (if there is one at all) is with users seeing cached copies of the pages they were looking at before. If you think that is a problem, turn caching off for the 'inside' pages (though I believe they can still see them in their history).
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

same problem is arising with me i just want it on only logout link but it is disabled back button for all links of that page...
Please tell me solution.................
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

Use This Code On Home Page Or Redirecting PAge Where You Redirect Control After Log out<html>
<head>
<title> Rizwan </title>
<script type="text/javascript">
function back_block() {
window.history.foward(-1)
}
</script>
</head>
<body önload="javascript:back_block();">
  Permalink  
v2
Comments
kanjolia gopal at 16-Jan-13 6:56am
   
Very Good Nice one........
markovl at 16-Jan-13 7:08am
   
What's up with resurrecting an already resolved question from 1.5 years ago? Why would you do that? Is it points? Is it boredom? What?
Kosimek at 25-Apr-14 15:04pm
   
@markovl Nothing was resolved 1.5 years ago (now 2.2 years ago). Instead of being insulting to someone posting a question to which to my knowledge no working, javascript independent, platform independent, cross-browser solution is available, I suggest that you either post such solution or refer to the page where such solution can be found.
You will do a gazillion people a big favour. I am certainly all ears!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Instead of Disabling the back button on the click of the logout button clear the cache
 
Response.ExpiresAbsolute = DateTime.Now;
      Response.Expires = 0;
      Response.CacheControl = "no-cache";
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100