If Deeplink url not work send user to download page

旧时模样 提交于 2021-01-27 04:49:35

问题


I need to implement a javascript where I can detect if my deep link is worked or not, If it works then it should remain same but if it does not work then it must start download file. For that, i use timeout function to do it. Here is sample code I used.

setTimeout(function () { window.location = "https://itunes.apple.com/appdir"; }, 25);
window.location = "appname://";

But this code works fine on android and ios but it is creating problem while it comes to the desktop browser. In desktop browser after Deeplink works properly, timeout function do not stops and it redirects to download page.

so finally I want some event which can detect if my Deeplink is worked or not so I can set cleartimeout function to prevent redirecting to downloading URL


回答1:


For desktop browser, consider using window blur listener and act accordingly Blur listener will tell you if user left the tab or browser

window.onblur=()=>{//deeplink check (maybe unsuccessfull?)
window.onfocus=()=>{//deeplink unsucesfull};

}



回答2:


I would try with a timestamp expression in the timeout.

Something like this (play around with the thresholds as needed):

var clickedTm = +new date;

setTimeout(function () {
   if (+new date - clickedTm < 600) {
      window.location = "https://itunes.apple.com/appdir"; 
   }
}, 500);

window.location = "appname://";



回答3:


I have been facing similar problem, and finally I have found a nice botched job to make it work:

var timer = null;

function setListener() {
    window.document.addEventListener("visibilitychange", function(e) {
        window.clearTimeout(timer);
        timer = null;
        window.open('','_self').close();
    });
}

function redirectAndroid() {
    setTimeout(function () { window.location = "https://itunes.apple.com/appdir"; }, 25);
    window.location = "appname://";
}

function redirecIOS() {
    setListener();
    var beforeApp = new Date().valueOf();
    window.location.href = "appname://";
    var afterApp = new Date().valueOf();
    if (afterApp - beforeApp < 100) {
        setTimeout(function() {
            "https://itunes.apple.com/appdir";      
        }, 25);
    } else {
        if (timer === null) {
            timer = setTimeout(function() {
                "https://itunes.apple.com/appdir";      
            }, 200);
        }
    }

This way, after redirecting to application, if it opens it triggers the event "visibilitychange" before the timeout function, and you clear the timeout avoiding it to redirect to web, and close the browser (if you want). If application is not installed, timeAfterApp - timeBeforeApp is not < 100 so there you set the timeout.



来源:https://stackoverflow.com/questions/48587860/if-deeplink-url-not-work-send-user-to-download-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!