简体   繁体   English

如何处理深度链接重定向无法跨浏览器

[英]How to handle deeplink redirect fails cross browser

I am attempting to redirect to a custom scheme to deeplink to an iOS and Android application. 我正在尝试重定向到自定义方案,以深层链接到iOS和Android应用程序。 Behaviour is different cross browsers so I am trying to find a consistent solution. 跨浏览器的行为是不同的,因此我试图找到一个一致的解决方案。 Below is my jQuery and behaviours in different browsers. 以下是我的jQuery和在不同浏览器中的行为。

$(document).ready(function (){

    console.log(window.location.href);

    window.location.href = window.location.href.replace('http','custom').replace('https','custom');

    //I am waiting 2 seconds for this to succeed before trying to launch 
      device app store or www site if on an unsupported device/web browser

    window.setTimeout(function(){

        console.log(navigator.userAgent.toLowerCase());

        if(navigator.userAgent.toLowerCase().indexOf("android") > -1){

            console.log('Android');
            window.location.href = 'https://play.google.com/store/apps/details?id=';

        }else if(navigator.userAgent.toLowerCase().indexOf("iphone") > -1){

            console.log('iOS');
            window.location.href = 'https://itunes.apple.com/us/app/';

        }else{

            console.log('Other');
            window.location.href = 'https://www.website.com';

        }
    },2000);
//wait for 2000 milliseconds before trying to redirect to stores. 
           //this is crap it would be better to wait for an error or respond
           //to the Chrome External Protocol Request dialog

    });

Chrome launches the External Protocol Request dialog. Chrome启动“外部协议请求”对话框。 If this isnt responded to within 2 seconds or if user clicks 'Do Nothing' the redirect works 如果在2秒内未响应,或者用户单击“不执行任何操作”,则重定向有效

Firefox just fails to handle the custom URL scheme and displays 'The address wasn't understood' Firefox只是无法处理自定义URL方案,并显示“地址不明”

IE Launches the App Store dialog and successfully redirects(!) IE启动“ App Store”对话框并成功重定向(!)

Safari launches Cancel/Choose Application/Search App Store dialog but does not redirect. Safari启动“取消/选择应用程序/搜索应用程序商店”对话框,但不重定向。 Any interaction results in 'Safari cant open the specified address'. 任何交互都会导致“ Safari无法打开指定的地址”。

It would be much better to capture an event (if possible) from the respective dialogs to trigger the redirect. 最好从各个对话框中捕获一个事件(如果可能)以触发重定向。 2 seconds wait is arbitrary and probably wont always work. 2秒的等待时间是任意的,可能不会一直有效。

Any help much appreciated. 任何帮助,不胜感激。

I got there in the end following this help: How to check if a custom protocol supported 最终,我得到了以下帮助: 如何检查是否支持自定义协议

This checks the deeplinking code exists and if not just does a redirect. 这将检查深层链接代码是否存在,如果不是,则仅重定向。 If it does it attempts to change window.href value and if that errors just do the normal redirect. 如果确实如此,它将尝试更改window.href值,并且如果该错误只是正常重定向。

$(document).ready(function (){

        console.log(window.location.href);

        var code = window.location.href.substr(window.location.href.lastIndexOf('/') + 1);

        console.log(code);

        if(code != undefined && code !=""){

            console.log('Code is '+code);

            try{

                window.location.href = window.location.href.replace('http','custom').replace('https','custom');

            }catch(e){

                console.log('In error catch');

                redirectStores();

            }

        }else{
            console.log('No code here..');

            redirectStores();
        }

    });

    var redirectStores = function(){

        console.log(navigator.userAgent.toLowerCase());

        if(navigator.userAgent.toLowerCase().indexOf("android") > -1){

            console.log('Android');
            window.location.href = 'https://play.google.com/store/apps/details?id=';//add app id

        }else if(navigator.userAgent.toLowerCase().indexOf("iphone") > -1){

            console.log('iOS');
            window.location.href = 'https://itunes.apple.com/us/app/';//add app id

        }else{

            console.log('Other');
            window.location.href = 'https://www.example.com';

        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在javascript或jQuery中处理浏览器关闭(交叉) - How to handle browser close (cross) in javascript or Jquery Protractor跨浏览器测试如何处理日志记录? - How to handle logging in cross-browser testing in Protractor? 如何处理重定向请求以响应浏览器上的下载文件。 - How to handle redirect request in react to download file on browser. 使用javascript history.back()在Safari中失败..如何让它跨浏览器? - Using javascript history.back() fails in Safari .. how do I make it cross-browser? 如何处理来自单页应用程序浏览器仅 webapp 的重定向响应 - How to handle redirect response from a Single Page Application browser only webapp Safari xhr(AJAX)请求w /跨域重定向失败 - Safari xhr (AJAX) requests w/ cross-domain redirect fails 对于具有历史记录的跨浏览器javascript重定向,有什么好的解决方案? - What is a good solution for cross browser javascript redirect with history? 处理本地存储集项目异常的最佳跨浏览器方式? - Best cross browser way to handle exceptions for localstorage set item? 独立的跨浏览器库来处理location.hash - independent Cross Browser Library to handle location.hash 跨浏览器AJAX和setTimeout()在IE中有效,但在Chrome / Firefox中失败 - Cross browser AJAX and setTimeout() works in IE but fails in Chrome/Firefox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM