Determine whether the current environment is in a mini program or WeChat (determine whether the current page is entered by a mini program)

1. First use the method that comes with the mini program to determine window.__wxjs_environment ==='miniprogram' is true, that is, the page entered by the mini program entry, but the Android sub-method is invalid

<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
function wxProgramExec(fn) {
    
    
        var url = location.search;
        if(window.__wxjs_environment === 'miniprogram' || url.indexOf("miniprogram") != -1){
    
    
            sessionStorage.setItem("wxjs_environment", "isTrue");
            fn()
        }else{
    
    
            sessionStorage.getItem("wxjs_environment");
            if(sessionStorage.getItem("wxjs_environment")){
    
    
                fn()
            }
        }
    }

2. Bring parameters (such as miniprogram) to the page entered by the mini program entrance, and judge on the page var url = location.search; url.indexOf("miniprogram") != -1 is true, which means that the mini program entry is entered page

The combination of the two can achieve the judgment that the current page is the page that the applet enters into

Guess you like

Origin blog.csdn.net/qq_39367226/article/details/88039775