Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to override back button?

I want to override back button in browser so that, for example, if a user came from A.aspx to B.aspx, when he tries to go back from B.aspx it should go to Home.aspx not A.aspx.

Is there any way to accomplish that? It doesn't matter if it's using ASP, C#, JavaScript or jQuery.

like image 472
Avinash Avatar asked Dec 19 '22 20:12

Avinash


2 Answers

You could use JQuery to catch the browser back (popstate) event.

$(document).ready(function () {
  if (window.history && window.history.pushState) {
    window.history.pushState('forward', null, null);
    $(window).on('popstate', function () {
      window.location.href = 'http://stackoverflow.com';
    });
  }
});
like image 184
Moe Avatar answered Dec 21 '22 09:12

Moe


Track the last page in the user's ASP.NET session. This can be done In C# by setting the "last page" on the session whenever an aspx page loads. When ASP.NET A.aspx loads, check the last page in the session before setting it. If it was B, redirect to home.

A guide to using session state in ASP.NET Web Forms: https://msdn.microsoft.com/en-us/library/ms178581.aspx

like image 27
MattTannahill Avatar answered Dec 21 '22 11:12

MattTannahill