如果javascript中不存在原生应用程序,则执行代码

Execute code if native app not exists in javascript

本文关键字:执行 代码 应用程序 原生 javascript 不存在 如果      更新时间:2023-09-26

我想显示一个弹出,如果instagram没有安装。我现在有这样的代码:

setTimeout(function () { 
            $.mobile.changePage( "dialog-instagram.html", { role: "dialog" });
}, 5);
window.location = "instagram://media?id=" + element.title;

如果手机上安装了instagram,这将启动应用程序并显示我想要的照片。如果没有安装,启动dialog-instagram.html

谢谢好了。问题是总是启动dialog-instagram.html。我只是希望它启动,如果应用程序没有打开。我明白为什么,代码会延迟5秒启动。

这能做到吗?我怎么知道是窗口。定位成功与否?

尝试增加延迟时间非常短,所以您可以尝试增加它(300ms将是合理的)。如果这不起作用,请尝试以下代码(基于此答案):

function startIThrown(){
  document.location = 'instagram://media?id=' + element.title;
  setTimeout(function(){
    if(confirm('You do not seem to have iThrown installed, do you want to go download it now?')){
      document.location = 'http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=293049283&mt=8&uo=6';
    }
  }, 300);
}
<a href="#" onclick="startIThrown()">Restart iThrown</a>