JS 使用window.location.href 报错怎么捕获?

新手上路,请多包涵

JS 使用window.location.href 唤起腾讯会议,但是本机没安装腾讯会议,所以提示了一个这,我想知道怎么可以捕获下面的异常,用来判断是否跳转腾讯会议APP下载页面,使用window.onerror 无效 window.addEventListener('error',function{}) 也无效,有没有jym遇到过这个问题?
1659952250769.jpg

1659952337495.jpg

阅读 5.2k
2 个回答

可以使用ajax 模拟类似 ping 之类的操作,确定地址可达的(statusCode 200)后,再进行重定向。

var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function() {
    if(xhr.readyState == 4 && xhr.status == 200) {
        window.location.href = 'wemeet://xxxxx/xxx';
    }
}

xhr.open('head', 'wemeet://xxxxx/xxx');
xhr.send(null);

代码是这样的

function callapp_PC({url, callback}) {
        var t = setTimeout(callback, 1000);
        var inp = document.createElement("input");
        inp.style.position = "absolute";
        inp.style.clip = "rect(0, 0, 0, 0)";
        // 出现下载框的时候往下跳的问题
        inp.style.top = '0'
        inp.style.left = '0'

        function blur() {
            window.clearTimeout(t);
        }

        inp.addEventListener("blur", blur); // 监听blur事件
        document.body.appendChild(inp);
        inp.focus(); // 获取焦点
        setTimeout(function () { // 删除无用的标签
            inp.removeEventListener("blur", blur);
            document.body.removeChild(inp);
        }, 1000);
        //有客户端 如果有本地exe应用,就会弹框,input失去焦点,然后执行blur()事件 清空t定时器 - 删除无用的标签定时器
        //无客户端 不会弹框,input也不会失去焦点 触发t定时器- 执行callback - 删除无用的标签定时器
        location.href = url;
    }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题