当使用javascript点击后退按钮时重定向到另一个网址 [英] Redirect to another url when back button is clicked using javascript

查看:109
本文介绍了当使用javascript点击后退按钮时重定向到另一个网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张付款表格,用户可以在其中输入他所有的卡片详细信息,当他点击时,他会被带到银行的3D安全页面。但问题是,用户只需点击浏览器的后退按钮,就可以返回到付款页面,如果他再次发起付款,则有可能发生多次交易和重复引用。 / p>

所以我的问题是:有没有什么方法可以将用户重定向到自定义页面,当他点击后退按钮时会显示Session expired,因此交易已被取消。 所以我们避免了refid的重复?

解决方案

使用下面的jquery重定向您自己的url时,点击浏览器后退按钮<如果(window.history&& amp;& amp; amp;& amp; amp; amp;& amp; amp; amp;& amp; amp; amp; ; 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';
返回false;
}
}
});

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

});


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?

解决方案

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

   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');
      }

    });

这篇关于当使用javascript点击后退按钮时重定向到另一个网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆