测试和打开应用程序脚本无法正常工作

问题描述 投票:0回答:1

我试图为处理程序创建一个测试器来打开应用程序,如果应用程序不存在,则打开下载页面。但使用我拥有的代码,它并不能完全按照我想要的方式工作。当应用程序未安装时,就会出现此问题。也许有人可以帮助我朝正确的方向前进。

<li onclick="openWebexContent()" class="li-slider">
WBX
</li>
function openWebexContent() {
    var appURL = "webex://";
    var fallbackURL = "https://webex.com/downloads.html";
    var appOpened = true;

    var iframe = document.createElement('iframe');
    iframe.style.display = 'none';
    iframe.src = appURL;
    document.body.appendChild(iframe);

    setTimeout(function () {
        if (!appOpened) {
            document.body.removeChild(iframe);
            var downloadConfirmation = confirm("Webex kon niet worden geopend! Klik op OK om het te downloaden.");
            if (downloadConfirmation) {
                window.open(fallbackURL);
            } else {
                alert("eeuhm een error");
            }
        }
    }, 1000);

    iframe.onload = function () {
        appOpened = false;
    };
}
javascript html
1个回答
0
投票

您不需要 iframe 来测试 url 的可用性。

这个方法更干净,更容易实现......

try{

 let Url = new URL(appURL);

 // all is good so proceed processing with appURL    

} catch(e){    

 // Url is not available so proceed with alternative fallbackURL    

}
© www.soinside.com 2019 - 2024. All rights reserved.