Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Disable browser back button functionality using JavaScript in asp.net | ASP.Net disable browser back button (using javascript)

Nov 30, 2011
Introduction

Here I will explain how to disable browser back button functionality using JavaScript in asp.net.

Description

One day I got requirement like disable browser back button to prevent users going back to previous page using back button of the browser. I search many sites for proper code to prevent users navigating back to previous page but there is no use because some of them work in IE but not in Mozilla and some of them will work in Mozilla but not in IE.

Here this JavaScript functionality will work in all browsers and prevent users navigating back to previous page by hitting on browser back button check below piece of JavaScript code


<script type="text/javascript" language="javascript">
function DisableBackButton() {
window.history.forward()
}
DisableBackButton();
window.onload = DisableBackButton;
window.onpageshow = function(evt) { if (evt.persisted) DisableBackButton() }
window.onunload = function() { void (0) }
</script>
We need to place above script in header section of a page wherever we need to prevent users navigate back to another page by using browser back button. 

I will explain our requirement with an example I have two pages Defaul1.aspx and Default2.aspx now I will redirect from Default1.aspx page to Defaul2.aspx page. After come from Defaul1.aspx page to Default2.aspx if I try to navigate back to Default1.aspx page from Defaul2.aspx then I want prevent user navigate back to previous page (Defaul1.aspx). To achieve this functionality place above JavaScript function in header section of required page. 

After add our JavaScript functionality to our page that code will be like this


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Disable Browser Back buttons</title>
<script type="text/javascript" language="javascript">

function DisableBackButton() {
window.history.forward()
}
DisableBackButton();
window.onload = DisableBackButton;
window.onpageshow = function(evt) { if (evt.persisted) DisableBackButton() }
window.onunload = function() { void (0) }
</script>
</head>
<body >
<form id="form1" runat="server">
<div>
First Page
</div>
<div>
<asp:Button id="btnFirst" runat="server" Text="Go to First Page" PostBackUrl="~/Default.aspx"  />
<asp:Button ID="btnSecond" runat="server" Text="Go to Second Page" PostBackUrl="~/Default2.aspx" />
<asp:Button ID="btnThree" runat="server" Text="Go to Third Page" PostBackUrl="~/Default3.aspx" />
</div>
</form>
</body>
</html>
Demo

We can also achieve this by disabling browser caching in code behind write the following lines of code in Page_Init event or Page_Load event and don’t forgot to add namespace using System.Web; because HttpCacheability related to that namespace.

protected void Page_Init(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
}
We need to place this code in a page wherever we need to disable browser back button. If we use above caching method to disable browser back button that will display webpage expired message like this 

  Download sample code attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

48 comments :

bhawna said...

thnxx i was searching for it from days....................

bhawna said...

i used the code but its not working

Suresh Dasari said...

hi bhawna,
i tested it in all browsers it's working fine you should place that JavaScript function in all the pages wherever you need to disable back button

bhawna said...

for master page and content page concept i want to disable back button on login page only which is one of the content page

Suresh Dasari said...

hi bhawna,
you should place JavaScript code in login page of content placeholder and try it will work for you

bhawna said...
This comment has been removed by the author.
bhawna said...

thnn its working...

sudheer said...

Hi Suresh This is sudheer trained .net developer.i am following ur portal daily and i am practicing most of the examples in ur blog, it is very useful to learn beginners.Can u post me Globalization/Localization concept with example please.

Mehtab Ali said...

Its a good post for the beginners..:)

narwal said...

hi suresh,

This example works very well athe pages
Please help me to explain that how we count the user online and when i close the the browse the user count actually that the total user online please help with example

Sourav said...

while clicking the back button it is showing the previous page for a glimpse...is it possible to remove it so it doesnt show the previous page and only stays on that page....????

actually i am talking about the thing that happens in gmail..after logging out it doest show me the login page for a glimpse

sory for my bad english....thanx in advance :)

Sourav said...

actually i want the code for logout that exactly happens in gmail or in other websites...

plz help me...i need it

thank u :)

Naveen said...

Hi sir..my requirment is i hav one form with 2 textboxes and submit button...i fill textboxes with values and submit means it ll save in database..the next time if i click the textbox to enter another record..the prevoiusly eneterd Value should not be dislayed..means all history content needs to be cleared..

Anonymous said...

can i use this code in html document having javascript????

Unknown said...

Please Sir ,
give me Video Chat Code

Unknown said...

this is useful and java script working but C# not working ...

thanks
Praveen Kumar S

Anonymous said...

thanks

Good one

Anonymous said...

It worked well , but you forced previous page to forward to current page , what if previous page is from another server? or what if i have only one page that is default.aspx . I mean how can i achieve the same thing when i face the situations i mentioned above ..
Thanks in advance
Cheers

Anonymous said...

awesome example ... good and keep it up.

Unknown said...
This comment has been removed by the author.
Unknown said...

hey...
i want to know how to give hyperlink to the gridview column through c# code... plx help me out

Unknown said...

thanx bro

Anonymous said...

Hi suresh i have tried the code you have given but still when we are clicking on the bowser back button it shows the previous page for a few micro second. plz provide the solution. Thanks a lot for the article...

Allen said...

Thanks your great post, my brother!

amazing said...
This comment has been removed by the author.
amazing said...

i used the code but its not working. I place JavaScript code in login page of content placeholder.Please help me..

Unknown said...

hello sir nice code u have posting ,
but i have one another problem i am working in asp.net 4.0 when user can login then he redirect to the inner page but when he click on logout button then he back to home page its fine and this code is work for back button but if user copy the inner page link and then logout he past that link so he again redirect to the inner page ,,,
plz solve my problem?

thanks

Mayur Agarwal said...

thanx a lot suresh
this code worked very well in my project
thank u very much...........

Unknown said...
This comment has been removed by the author.
pranav said...

hii Suresh... I just want the same functionality like Facebook has....
As in Facebook... whenever we logged in successfully, then we entered in home page....
And now click on the back button of the browser and still we are stay on the home page it self.... without going to the login page of the Facebook.

Please help me asap.
my email ID is: pranavpatel1991@gmail.com

Anonymous said...

thanks

Anonymous said...

hi.myself sandhya ...thank you this post was helpful to me.....

Anonymous said...

Hi,
This concept is working for me. But i face one problem.i want to restrict with out clearing my entered data.for example i entered some thing in my page controls than those controls cleared.

Unknown said...

i want to delete browser history when page load...

Unknown said...

to disable back action...just put the following script inside head in master page or all page.

.......<..head..>
<..style>
<..script type="text/javascript">
history.forward();
<../script>
<../head..>...........

Unknown said...

while clicking the back button it is showing the previous page for micro second...is it possible to remove it so it doesnt show the previous page and only stays on the current page only....????

Anonymous said...

This code is not working in IE8

Unknown said...

onpageshow never fires when click back button, windows 8.1 ie11.

Prashanth said...

Thank you sir ,This code worked like a charm..

Anonymous said...

but if you click on the refresh button (from the page which shows in the sample image), it will reload the contents again.
Im looking for something like this but it shouldnt be able to refresh / or get error message (or go to the login page) when you refresh the browser from the page where the back button took you

Unknown said...

Thank You suresh.Your trick worked in my web application.your articles always help me a lot.keep it up

S. Murugan said...

great....

Unknown said...

It is not working...

DSS said...

Thanx

Unknown said...

thanx a lot the code work for me

Anonymous said...

Hello Sir,
DisableBackButton function is not working for me,I have 3 files login.html,profile.php,logout.php and even login.php also present so I want to prevent users from coming back after logout to there profile page.r

I have blindly copied your code between head tag of the login.html and profile.php,but no result.

PLease help me out

Anonymous said...

Thank you it works well,but it shows a glimpse of the the previous page when back button is pressed.

Can anyone suggest a way to prevent the above mentioned scenario??

P.anil kumar said...

thanks so much it works

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.