Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling backbutton of browser in C#

Tags:

c#

asp.net

<script type="text/javascript">
    {
        function DisableBackButton() {
            window.history.forward()
        }

        DisableBackButton();

        window.onload = DisableBackButton;
        window.onpageshow = function (evt) { if (evt.persisted) DisableBackButton() }
        window.onunload = function () { void (0) }
    }
</script>

I am using the following code in the master page to diable the back button.

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();

Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
Response.Expires = -1500;
Response.CacheControl = "no-cache";
Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);

I have a master page in that logout button is there once user click on that user will be redirected to logout page. This is working fine and once I am clicking on back button it is taking me to the last page I browsed. Even I tried with JavaScript.

I am creating timing out the session after 5 minutes. When session expires user will be redirected to session expiry page there also backbutton is taking me to the last page browsed.

like image 758
Firoz Gandhi Avatar asked Aug 02 '13 05:08

Firoz Gandhi


People also ask

Can I disable browser back button?

You can-not actually disable the browser back button. However, you can do magic using your logic to prevent the user from navigating back which will create an impression like it is disabled.

Can we disable Chrome back button?

Select Settings from the list. Scroll down to the Privacy and Security section, and select the Site settings from the menu. Choose the Pop-ups and redirects option within Site settings. Toggle the button to turn OFF and block the pop-ups and redirection.

How do I disable back forward and refresh functionality in browser?

function disableBackButton() { window. history. forward(); } setTimeout("disableBackButton()", 0);


2 Answers

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>

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

like image 90
Mahesh Avatar answered Sep 29 '22 22:09

Mahesh


<script type="text/javascript" language="javascript">
    window.onload = function () {
        noBack();
    }
    function noBack() {
        window.history.forward();
    }
</script>
<body  onpageshow="if (event.persisted) noBack();">
</body>

Hello, You can do it like this,

Implement this code in master page

I have implemented this and it worked for me..

like image 29
salah9 Avatar answered Sep 29 '22 22:09

salah9