Link to home
Start Free TrialLog in
Avatar of kousis
kousis

asked on

Disable back button in Browser

hi,
 I need to disable the back button in browswer for security. Is it possible.. please guide me
TIA
Avatar of rumblefiz
rumblefiz

as far as i know it is not possible to disable the back button. if it is critical that a user not go back to a page, i use javascript to remove the page from history. that way when the user presses back, they go to the page before the page you dont want them to see. example:

page1.htm
<html>
<head>
   <title>Page 1</title>
   <script language="javascript">
      location.replace("/index.html");
   </script>
</head>
<body>
All your other stuff
</body>
</html>

now when someone loads page1.htm for the first time, they will see whatever you have in the body. when they submit or go to another page, when they press the back button, index.html will load instead. this does not make the entry in the history list disappear if you click on go | etc... but it does replace the doc with the one you specify. if you dont specify one, i think it may just remove it and they will go back to the page they were on before they loaded page1.html.

this worked for me and it may work for you. may not though.

- rumblefiz
It is not possible to do that.

Read this for some workarounds of it.
http://www.4guysfromrolla.com/webtech/111500-1.shtml
http://www.4guysfromrolla.com/webtech/111500-1.2.shtml

hongjun
ASKER CERTIFIED SOLUTION
Avatar of hongjun
hongjun
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Michel Sakr
You can't do it directly, but her's a work around:

<SCRIPT LANGUAGE=javascript>
{
 var Backlen=history.length;
 history.go(-Backlen);
 top.window.location.href ="YourPage.asp";
}
</SCRIPT>

or this:

<body OnLoad="window.history.forward();">

this way you disable the ability to move backwards.


In conclusion, you cannot really disable the back button. You just have to resort to using workarounds. Combine mine and Silvers5's and you will get what you wanted.

hongjun
<script language="JavaScript">
<!--
  javascript:window.history.forward(1);
//-->
</script>
pack,

What is the difference between your

<script language="JavaScript">
<!--
 javascript:window.history.forward(1);
//-->
</script>

and Silvers5's

<body OnLoad="window.history.forward();">


hongjun
kousis,
please reject pack's answer and consider mine or Silvers5's.

hongjun
or split for both
But first of all the questionner got to reply.

hongjun
Avatar of kousis

ASKER

while using location.replace() page is getting refreshed for each sec. i don't know where the problem is. could u pls suggest me
Why don't you use document.replace instead? First of all, reject his answer.

hongjun
Avatar of kousis

ASKER

while using location.replace() page is getting refreshed for each sec. i don't know where the problem is. could u pls suggest me
Pack - since you're new to Experts-Exchange I would suggest you carefully read through the section regarding questions and answering questions here : https://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp

In brief. Do not post your comment as an answer unless it completely provides a solution to the person posting the question and that it has NOT already been suggested by another user as a comment.

Ideally all users should only post comments as it allows the person who posted the question to select the correct person for points awarding. This also keeps the question in the un-answered questions section so that other experts can see it and post a reply. Otherwise it gets moved to the Locked questions section.

In answer to your question pretty much everyone has given the same answer as there is no way of really "disabling" the back button except for resorting to work-arounds. If you don't want users to use browser navigation other than that which you provide on your site then I'd suggest opening your site in a toolbar-less window as well as disabling the right click. Then you MUST however make sure that your navigation is 100% adequate and easy to use.

Regards,
CyberSoft
pack I am rejecting your answer as it duplicates comments proposed by other members.

Lunchy
Friendly Neighbourhood Community Support Moderator

Avatar of kousis

ASKER

document.replace() is giving runtime error
"object doesn't support this property"
Should be location.replace('filename.html') NOT document.replace('filename.html')

Regards,
CyberSoft
Try
window.replace("index.html");

hongjun
Avatar of kousis

ASKER

like rumblefiz suggested, i am giving this window.replace in head, but it's printing the page which i am specified in the url first not the page which i gave in the <body>

so suggest where i ahve to use the code.
TIA
Try this

first_page.asp
=============
Click <a href=javascript:window.replace("second_page.asp")>here</a>

hongjun
Avatar of kousis

ASKER

It is working while i am giving it in onClick()
In my case.
I got 2 frames
in one frame i have logout option. After clicking the logout option it goes to main screen then if i click back in the browser it goes to the prevoius page which i don't want to. I want to redirect to some other page.
Hope u understand my query and clear my doubt

TIA
<ping>
Avatar of kousis

ASKER

Lunchy
what is that mean?
Sorry kousis, I didn't realize I was getting notifications on this question so I posted a ping comment to make sure I was getting them.
try

document.location.replace("url")
any progresses?

hongjun