Home >Web Front-end >H5 Tutorial >Click the return button to close the current page and window in WeChat, Alipay, and Baidu Wallet

Click the return button to close the current page and window in WeChat, Alipay, and Baidu Wallet

黄舟
黄舟Original
2017-03-28 13:58:525340browse

Recently, I am using WeChat, Alipay, and Baidu Wallet to implement web payment. If the payment is successful, the page will be automatically closed. If the payment fails, an error message will be displayed. When on the error page, click Return

or the Android physical button to go back, the page will be closed.

In WeChat, Alipay, and Baidu Wallet, they encapsulate page closing. The traditional window.close() is invalid, and their js code must be used to close it. The following are three ways to close mobile apps

:

WeixinJSBridge.call('closeWindow');//微信  
 AlipayJSBridge.call('closeWebview');  //支付宝  
 BLightApp.closeWindow();//百度钱包

Determine which browser it is by the browser header:

var ua = navigator.userAgent.toLowerCase();    
f(ua.match(/MicroMessenger/i)=="micromessenger") {    
      alert("微信客户端");  
} else if(ua.indexOf("alipay")!=-1){    
 alert("支付宝客户端");   
}else if(ua.indexOf("baidu")!=-1){    
 alert("百度客户端");   
}

For return, previous page, and back Monitor and put the current page address in the history:

$(function(){  
            pushHistory();  
            window.addEventListener("popstate", function(e) {  
                  
        }, false);  
            function pushHistory() {  
                var state = {  
                    title: "title",  
                    url: "#"  
                };  
                window.history.pushState(state, "title", "#");  
            }  
});

The complete code for the entire implementation:

$(function(){  
            pushHistory();  
            window.addEventListener("popstate", function(e) {  
                pushHistory();  
                var ua = navigator.userAgent.toLowerCase();    
                if(ua.match(/MicroMessenger/i)=="micromessenger") {    
                     WeixinJSBridge.call('closeWindow');  
                } else if(ua.indexOf("alipay")!=-1){    
                     AlipayJSBridge.call('closeWebview');    
                }else if(ua.indexOf("baidu")!=-1){    
                    BLightApp.closeWindow();  
                }  
                else{  
                    window.close();  
                }  
        }, false);  
            function pushHistory() {  
                var state = {  
                    title: "title",  
                    url: "#"  
                };  
                window.history.pushState(state, "title", "#");  
            }  
        });

The above is how to click the return button to close the current page and window in WeChat, Alipay, and Baidu Wallet For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!

Related articles:

Summary of several ways to close the current page (window) in js

Automatically after the specified time JS code to jump or close the current page

Open a new window and close the current page without popping up the closing prompt. JS code

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn