Redirect to another url when back button is clicked using javascript

匿名 (未验证) 提交于 2019-12-03 02:05:01

问题:

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?

回答1:

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


回答2:

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

Handling pressing back button :

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


回答3:

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

Then use the script like this:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!