0

The URL I need the mobile site redirected to: Deeplink (wake up mobile app). The following code is unable to do the redirect, while manual touch on the button can redirect.

window.location.replace(url); 

Next I've tried to trigger the click event programmatically. No idea why is it not working. Refer to all the answer in this question - How can I programmatically invoke an onclick() event from a anchor tag while keeping the ‘this’ reference in the onclick function?

The following is my code.

document.getElementById('BtnClick').href=url;
(function() {
    var app = {
    launchApp: function() {
        window.location.replace(url);
        this.timer = setTimeout(this.openWebApp, 300);
        var elem = document.getElementById("BtnClick");
        if (typeof elem.onclick == "function") {
            elem.onclick.apply(elem);
        }
    },

    openWebApp: function() {
        console.log("InOpenWebApp");
        if(mobileUtil.isIOS){
            window.location.replace(iosurl);
        }else if(mobileUtil.isAndroid){
            window.location.replace(androidurl);
        }
    }
};
app.launchApp();
})();

What's the possible reason or syntax error?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
1myb
  • 3,536
  • 12
  • 53
  • 73
  • #1, try element.click() , #2 when the setTimeout is executed the current window variable is garbage collected, elements that are in it cannot be referred, #3 a minimal working example that reproduces the issue in js fiddle would help – ibrahim tanyalcin Feb 18 '19 at 09:07
  • @ibrahimtanyalcin I've tried element.click() not working. Removed setTimeout too. Running on js fiddle is not working because the app is not available. – 1myb Feb 18 '19 at 09:17
  • https://jsfiddle.net/ibowankenobi/5c0dxp18/ as you can see, it works, both setTimeout or the first call to replace url will trash the memory. If you uncomment them, the console log will fire. What do you want to do with all this? – ibrahim tanyalcin Feb 18 '19 at 09:25
  • maybe the function that does the url replace just isnt getting called oO – jonathan Heindl Apr 02 '19 at 17:51

0 Answers0