Empty page redirection

Time:2022-5-26
  • There is a need to obtain the third-party page through the interface. Because the third-party page will change at any time, it is not easy to make the app write dead, so you can write a blank page through H5 and jump after obtaining the third-party page.

There are two problems

After Android jumps to the third-party page, click back to return to my blank page, and then jump to the third-party page again

The reload event will not be triggered when IOS returns my blank page

  1. For the first question, I handle it through the URL. When the app starts to enter my page, it will take parameters, and thenhistory.pushStateMethod to remove the parameters. In this way, when Android returns to my page, I judge whether to return or enter the processing for the first time through the URL parameter.
  2. The second problem is that IOS will not refresh the page again after it returns through listeningpageshowEvent in which the returned event is processed.

The code is as follows

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
        content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="format-detection" content="telephone=no">
    <title></title>
</head>

<body>

</body>
<script>
    var u = navigator.userAgent
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
    //Used for app transit to jump to a third party

    var hash = location.hash
    //IOS returns to the previous page without refreshing
    if (isiOS) {
        window.onload = function () {
            var isPageHide = false;
            window.addEventListener('pageshow', function () {
                if (isPageHide) {
                    //Closing container method of self app package
                    window.webkit.messageHandlers.NativeFunction.postMessage(JSON.stringify({
                        'command': 'closePageAction',
                        'parameter': ''
                    }))
                }
            });
            window.addEventListener('pagehide', function () {
                isPageHide = true;
            });
        }

    }
    //First entry
    if (hash) {
        //Remove the parameters on the URL
        history.pushState({}, '', location.origin + location.pathname)
    } else {
        //IOS won't enter here
        if (isAndroid) {
            //Call the method encapsulated by your app and close the container after approval
            window.jsobj.NativeFunction(JSON.stringify({
                'command': 'agree'
            }))
        }

    }

    //Interface to get the third-party page and jump 
    var ajax = new XMLHttpRequest();
    ajax.open('get', '/getgroup');
    ajax.send();
    ajax.onreadystatechange = function () {
        if (ajax.readyState == 4 && ajax.status == 200) {
            var res = JSON.parse(ajax.response)
            if (res.code == 0) {
               //The interface jumps to different pages through hash
                res.data.forEach(function (item) {
                    if (item.agreementCode == 'tk_customerprivacy_agreement' && hash == '#1') {
                        window.location.href = item.agreementUrl
                        return false
                    }
                    if (item.agreementCode == 'tk_customerservice_agreement' && hash == '#2') {
                        window.location.href = item.agreementUrl
                        return false
                    }
                })
            }
        }
    }
</script>

</html>

Recommended Today

Swift function (method)

1、 Definition of function be careful ⚠️: By default, the function parameter is let, and the value of the parameter cannot be changedForm of function Function parameter name (func) – > (return value){ Return return value } Function parameters are optional, and the number of parameters can be one or moreThe return value of the […]