最近项目中需要在页面上判断用户电脑上是否安装某个应用程序,类似于手机上浏览器上的通过微信登录,然后页面就会去检测手机是否安装微信app的功能。
例如检测电脑上行是否安装QQ,腾讯的Tencent://Message协议注册表如下:
[HKEY_CLASSES_ROOT\TENCENT] @=”TencentProtocol” “URL Protocol”=”D:\\Program Files\\Tencent\\QQ\\Timwp.exe”
[HKEY_CLASSES_ROOT\TENCENT\DefaultIcon] @=”D:\\Program Files\\Tencent\\QQ\\Timwp.exe,1″
[HKEY_CLASSES_ROOT\TENCENT\shell]
[HKEY_CLASSES_ROOT\TENCENT\shell\open]
[HKEY_CLASSES_ROOT\TENCENT\shell\open\command] @=”\”D:\\Program Files\\Tencent\\QQ\\Timwp.exe\” \”%1\”"
此注册表所实现的就是当浏览器碰到 tencent://… 时,自动调用 Timwp.exe,并把 tencent://… 地址作为第一个参数传递给 Timwp.exe。
提示success则表示已安装。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<iframe id="trigger_protocol_ifrm" style="display:none"></iframe>
<input id="protocolTxt" type="text" value="tencent://" />
<input id="checkBtn" type="button" value="check" />
<script>
document.getElementById('checkBtn').onclick = function(){
var url = document.getElementById('protocolTxt').value;
launchApplication(url, function(){
alert('success');
}, function(){
alert('fail');
});
};
var isDone = true;
var timeout;
var assistEl = document.getElementById('checkBtn');
var triggerEl = document.getElementById('trigger_protocol_ifrm');
function done(){
isDone = true;
assistEl.onblur = null;
triggerEl.onerror = null;
clearTimeout(timeout);
}
function launchApplication(url, success, fail){
if(!isDone)return;
isDone = false;
assistEl.focus();
assistEl.onblur = function(){
if(document.activeElement && document.activeElement !== assistEl){
assistEl.focus();
} else {
done();
success();
}
};
triggerEl.onerror = function(){
done();
fail();
};
try{
triggerEl.src = url;
}catch(e){
done();
fail();
return;
}
timeout = setTimeout(function(){
done();
fail();
}, 3000);
}
</script>
</body>
</html>
参考链接:
https://blog.youkuaiyun.com/zhangyani_502000/article/details/81009907