javascript 使用javascript单击后退按钮时重定向到另一个网址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20771045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:15:39  来源:igfitidea点击:

Redirect to another url when back button is clicked using javascript

javascriptphphtml

提问by user3128920

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.

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

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?

所以我的问题是:当用户单击后退按钮时,是否可以通过某种方式将用户重定向到自定义页面,该按钮显示“会话已过期,因此交易已被取消。” 这样我们就可以避免重复引用 ID?

回答by Sankar

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

单击浏览器后退按钮和 ipedia.com 时,使用以下 jquery 重定向您自己的 url

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

    });

回答by Kirill

You have to use history api, and for better compatibility use history.js plugin.

你必须使用 history api,为了更好的兼容性,使用history.js 插件

Handling pressing back button :

处理按下后退按钮:

$(window).bind('onpopstate', function(e){
   //your dark doings here
});

回答by Ashok Kumar Nath

You can use Brooke's modified script. That's a long script (should be used as external file). You can download it here!

您可以使用 Brooke 修改后的脚本。这是一个很长的脚本(应该用作外部文件)。你可以在这里下载!

Then use the script like this:

然后像这样使用脚本:

bajb_backdetect.OnBack = function(){
    document.location.href = 'http://google.com';
}

回答by Bijendra Sahu

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

如果用户单击后退按钮,这会将您重定向到您指定的页面(100% 工作)

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
    }
}