1

Possible Duplicate:
How to disable Back button in IE and firefox?

I am developing an one page website(DAM). To enter this site user have to be authenticated. After login if user click on the back button, he is seeing the login page. The login page is not in my control, means it is provided by the oracle content management system, so I cannot add any code there.

What code should I add to my page so that if one pressed the back button he will be navigated to the same page.

I have tried to use onbeforeunload and that worked, but I don't want any confirm message, just wanted to stay on that page.

Community
  • 1
  • 1
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
  • 1
    @RosdiKasim Have you read past the title? – Rob W Jul 25 '12 at 12:21
  • It isnt a very good idea to override browser controls. At best you could signal to the user that they are about to leave the site and give them the option to stay. – Hunter McMillen Jul 25 '12 at 12:21
  • There is no simple code. If you can use location.replace then that is the best solution – mplungjan Jul 25 '12 at 12:21
  • 1
    This concept keeps coming up over and over again. What is this immolating passion people seem to have for overriding browser controls? – Joel Etherton Jul 25 '12 at 12:23
  • @RosdiKasim: Yeah I agree with RobW: You apparently not even read the title correctly. This has nothing to do with disabling back button functionality. **NOTHING**. – Robert Koritnik Jul 25 '12 at 12:56
  • @RobertKoritnik, isn't the intention the same? Doesn't matter how you are wording it, the intention is the same. They do not want the button to work. – Rosdi Kasim Jul 25 '12 at 13:33
  • @RosdiKasim: No it's not about disabling back button functionality, but rather putting an additional history entry after login. I would likely rather do a double back after login, which would get me back to original page that doesn't have any application-related history anymore... And reload it. But I disagree with you. It's not about **avoiding** back button functionality but rather about **mitigating** its functionality to prevent invalid application state (which in this case would be since user already logged in). And singe-page applications should use hashing anyways. – Robert Koritnik Jul 25 '12 at 14:08

6 Answers6

4

This is not possible as it is explicitly designed to work that way - if the user hits "back" the browser must return to a previous page. There are a few unreliable techniques out there but ultimately they all fail for one reason or the other.

However... I guess in your case you can do a double-redirect and handle the "bacK" button in your landing page that is immediately after the login?

YePhIcK
  • 5,816
  • 2
  • 27
  • 52
  • +1 Ever go to a page that is redirected to an advertisement that tries desperately to keep you from leaving? "WAIT! If you stay, we'll give you..." Any page that attempts to stop my navigation is just as frustrating, no matter the intentions. – Nick Jul 25 '12 at 12:24
2

How single page apps with good usability work

If you want to increase user experience and usability factor of your page, you should be using page hashing/fragments. I'm not going to provide the same (or similar) code that others did, but rather upgrade the experience.

So how about that UX then

Your single-page web application should be changing hash every time when it changes context of its work. That may be defined by user interface or business process that user does in the UI or maybe even individual steps of such processes when it's expected that users would/should be going back to previous process states.

This way your application would function similar to normal web applications that consist of several pages and create a new history entry on the same basis of business processes and their steps (steps in this case are usually full-page postbacks).

So business processes and thinking of your application as a normal multi-page one will give you many useful points when you may change page fragment. Since you can control this you can provide better granularity (usually less) than multi-page web applications.

A simple example where multi-page application history has too many points

Form posts are usually always problematic, so having a history point after postback response is always bad because users hitting F5 resend the same form over and over again (ok browsers do ask you about resending post data but that's still bad experience).

Page fragmenting is the way to go, just use it further than just preventing getting back to logon page.

Completely preventing logon page getting into history stack would be by displaying it on your page within an iframe. You would of course need to detect unauthenticated user and add the iframe yourself and load login in it. After user authenticates you should reload your own page. But you should be aware that detecting login success won't be a straight-forward process.

Community
  • 1
  • 1
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
0
window.location.replace(URL);

Replace the current document with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.

djserva
  • 395
  • 1
  • 11
  • From where should I invoke it? – Tapas Bose Jul 25 '12 at 12:39
  • Sorry, I didn't pay attencion about the login page is on OCM. In this case, the easier way is call a window.open() in the page after login to display your application e close the login page. – djserva Jul 25 '12 at 13:24
-1
function win_onkeydown_handler()
{
switch (event.keyCode){

case 116 : // 'F5'
event.returnValue = false;
event.keyCode = 0;
alert("Refresh Not Allowed");
break;
case 08 : // 'BS'
if(event.srcElement.tagName=="INPUT" || event.srcElement.tagName=="DIV")
{
}
else{
event.returnValue = false;
event.keyCode = 0;
//alert("Backspace Not Allowed");
}
break;
case 17 : // ctrl key   
 event.returnValue = false;
 event.keyCode = 0;
 alert('Control Key restricted');
 }
 }

Even i got this in my project but i use to block this on that page using the above script... if u need to block back space u can use that code alone

sasikals26
  • 835
  • 2
  • 18
  • 41
-2

You are not allowed to control client browser.. Due to some security issues, scripts are not allowed to control client browser..

Jayamurugan
  • 1,757
  • 2
  • 14
  • 34
  • That's a useless answer. You don't even offer arguments *why* one "is not allowed to control client browser". – Rob W Jul 25 '12 at 12:25
  • Due to some security issues, scripts are not allowed to control client browser.. – Jayamurugan Jul 25 '12 at 12:28
  • 2
    Due to security issues AntGuider is not permitted to provide a more in-depth answer. That's how industry operates these days... NOT. –  Jul 25 '12 at 12:54
-3

Try this, it will solve your issue:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="Back" onclick="history.go(0);return true;" />
</body>
</html>

Cheers!!!!

GOK
  • 2,338
  • 6
  • 34
  • 63