Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to another url when back button is clicked using javascript

I have a payment form in which user can enter all his card details,and when he clicks,he is taken to the banks 3D secure page. But,the problem is, the user can simply click on the back button of the browser and can go back to payment page, if he initiates a "pay now" again,there is a chance of multiple transaction and duplication of ref ids.

So my question is: is there some way I can redirect the user to a custom page when he clicks on back button which says "Session expired, so transaction has been cancelled." so that we avoid duplication of ref ids?

like image 595
user3128920 Avatar asked Dec 25 '13 09:12

user3128920


People also ask

How do I redirect another page when the login button is clicked?

By using HTML Anchor Tags <a>.. </a>, you can Redirect on a Single Button Click [html button click redirect]. To use HTML Anchor tags to redirect your User to Another page, you need to write your HTML Button between these HTML Anchor Tag's starting <a> and Closing </a> Tags.

How do I redirect a URL to another URL?

Redirects allow you to forward the visitors of a specific URL to another page of your website. In Site Tools, you can add redirects by going to Domain > Redirects. Choose the desired domain, fill in the URL you want to redirect to another and add the URL of the new page destination.

What allows you to redirect to another page after clicking?

To redirect one HTML page to another page, you need to add a <meta> tag inside the <head> section of the old HTML page.


2 Answers

Use the below jquery for redirect your own url when clicking browser back button andipedia.com

   jQuery(document).ready(function($) {

      if (window.history && window.history.pushState) {

        $(window).on('popstate', function() {
          var hashLocation = location.hash;
          var hashSplit = hashLocation.split("#!/");
          var hashName = hashSplit[1];

          if (hashName !== '') {
            var hash = window.location.hash;
            if (hash === '') {
              alert('Back button was pressed.');
                window.location='www.example.com';
                return false;
            }
          }
        });

        window.history.pushState('forward', null, './#forward');
      }

    });
like image 59
Sankar Avatar answered Oct 05 '22 23:10

Sankar


If user clicked back button this will redirect you to your specified page (100% Working)

window.history.pushState({page: 1}, "", "");

window.onpopstate = function(event) {
    if(event){
        window.location.href = 'https://www.google.com/';
        // Code to handle back button or prevent from navigation
    }
}
like image 34
Bijendra Sahu Avatar answered Oct 06 '22 01:10

Bijendra Sahu