Handling browser "Back" button using ASP.NET
To disable browser "Back" button
1) Very first and simplest way to disable the browser back button is
<script language="JavaScript">
javascript:window.history.forward(0);
</script>
Using this user can't use back button from browser to see previous page.
2) Here if user redirect to error page then on the body onload we can call the following function so that user can not use back button at all, here we are redirecting same page again and again.
<script language="JavaScript">
function DisablingBackFunctionality()
{
var URL;
var i ;
var QryStrValue;
URL=window.location.href ;
i=URL.indexOf("?");
QryStrValue=URL.substring(i+1);
if (QryStrValue!='X')
{
window.location=URL + "?X";
}
}
</script>
To close the browser window
Following are the alternate to handle browser "Back" button, like if you have an error in your application and you are redirecting to error page now from this error page user should not use browser back button to see the previous page.
We have to close the window by giving some alert error message, below some script to do so
1) To close the browser use
window.close() OR this.close() OR document.close()
2) To close the browser without prompt use
window.opener = top;
window.close(); OR this.close() OR document.close()
3) If you want to give some alert message to close the page and if user click "OK" then close the browser then call following function in body onload="check_form()"
<script language="JavaScript">
var submitted=false;
function check_form()
{
if (submitted == true)
{
alert ("Form already submitted") ;
return false;
}
else
{
submitted = true ;
document.frmError.submit();
window.status ="Session expired. Please restart";
window.opener = top;
alert("Please close this window.");
window.close();
}
}
</script>
4) If you want to close the browser automatically after few second then use following script in your aspx page
<script language="JavaScript">
var bloodythief = 5;
{
alert("Window will close after 5 second.");
window.setTimeout("window.close()",bloodythief*1000);
window.opener = top;
}
</script>
5)If you want ot disable browser Referesh or F5 button then use following script in your aspx page
<script>
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;
}
}
</script>
Redirect to login page
6) All the above solution not seem to be compatable in other browser (Mozilla,Netscape etc), actually Window.close() function not works properly in others browser.To work in properly in other browser you have to close the parent window then open new window, it's kind of redirecting to login page if session expire.
This Supports all browsers.
<script language="JavaScript">
function RedirectToLoinPage()
{
window.opener=self;
var URL = window.location.href;
if (URL.indexOf('Session') == -1)
{
alert('Not authorized to view this application.');
}
else
{
alert('Session expired.');
}
window.close();
window.open('Login.aspx','Login','');
}
</script>
8) Another way to close the browser in other browsers
<script language="JavaScript">
function closeMe()
{
var win=window.open("","_self");
win.close();
}
</script>
Imp: if the user has disabled JavaScript, then above java script won't work.
When user use "back" button to see previous page and submitting that page again then handle in the code behind also and redirect him to login page.
Session.Abandon() method to expire session and check in each page page_load event if (!Session.IsNewSession && Session["UserId"] == null) if session is null then redirect to login page.
Get the Yahoo! toolbar and be alerted to new email wherever you're surfing.
1 comment:
Hi Ritesh,
What I want to do is
Suppose you are on login page then you do login and moved to the next page. from the next page, you press back button. I want to show my own page then you have pressed the back button.
How will I achieve this?
Post a Comment