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
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. |
|||
|
|||
48 comments :
thnxx i was searching for it from days....................
i used the code but its not working
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
for master page and content page concept i want to disable back button on login page only which is one of the content page
hi bhawna,
you should place JavaScript code in login page of content placeholder and try it will work for you
thnn its working...
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.
Its a good post for the beginners..:)
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
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 :)
actually i want the code for logout that exactly happens in gmail or in other websites...
plz help me...i need it
thank u :)
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..
can i use this code in html document having javascript????
Please Sir ,
give me Video Chat Code
this is useful and java script working but C# not working ...
thanks
Praveen Kumar S
thanks
Good one
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
awesome example ... good and keep it up.
hey...
i want to know how to give hyperlink to the gridview column through c# code... plx help me out
thanx bro
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...
Thanks your great post, my brother!
i used the code but its not working. I place JavaScript code in login page of content placeholder.Please help me..
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
thanx a lot suresh
this code worked very well in my project
thank u very much...........
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
thanks
hi.myself sandhya ...thank you this post was helpful to me.....
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.
i want to delete browser history when page load...
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..>...........
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....????
This code is not working in IE8
onpageshow never fires when click back button, windows 8.1 ie11.
Thank you sir ,This code worked like a charm..
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
Thank You suresh.Your trick worked in my web application.your articles always help me a lot.keep it up
great....
It is not working...
Thanx
thanx a lot the code work for me
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
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??
thanks so much it works
Note: Only a member of this blog may post a comment.