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

Mar 28, 2017 pm 01:58 PM

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();//百度钱包
Copy after login

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("百度客户端");   
}
Copy after login

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", "#");  
            }  
});
Copy after login

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", "#");  
            }  
        });
Copy after login

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 (www.php.cn)!

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem of 'Undefined array key 'sign'' error when calling Alipay EasySDK using PHP? How to solve the problem of 'Undefined array key 'sign'' error when calling Alipay EasySDK using PHP? Mar 31, 2025 pm 11:51 PM

Problem Description When calling Alipay EasySDK using PHP, after filling in the parameters according to the official code, an error message was reported during operation: "Undefined...

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

What is the difference between H5 page production and WeChat applets What is the difference between H5 page production and WeChat applets Apr 05, 2025 pm 11:51 PM

H5 is more flexible and customizable, but requires skilled technology; mini programs are quick to get started and easy to maintain, but are limited by the WeChat framework.

What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? Apr 01, 2025 pm 10:48 PM

Compatibility issues and troubleshooting methods for company security software and application. Many companies will install security software in order to ensure intranet security. However, security software sometimes...

How to solve the problem of JS resource caching in enterprise WeChat? How to solve the problem of JS resource caching in enterprise WeChat? Apr 04, 2025 pm 05:06 PM

Discussion on the JS resource caching issue of Enterprise WeChat. When upgrading project functions, some users often encounter situations where they fail to successfully upgrade, especially in the enterprise...

How to choose H5 and applets How to choose H5 and applets Apr 06, 2025 am 10:51 AM

The choice of H5 and applet depends on the requirements. For applications with cross-platform, rapid development and high scalability, choose H5; for applications with native experience, rich functions and platform dependencies, choose applets.

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

See all articles