最近公司有个项目里面有个功能需要在PC网页调用本地应用程序,之前经常见到这样的功能,比如有的网页会打开QQ,下载链接会打开迅雷。于是到网上开始搜索相关解决方案,发现搜索的结果不理想,都不是很完整的教程,这里记录一下自己的实践。

首先注册一个本地注册表文件,指向本地应用程序路径

创建一个注册表文件myprotocol.reg,内容如下所示。

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\myprotocol]

@="myprotocol Protocol"

"URL Protocol"=""

[HKEY_CLASSES_ROOT\myprotocol\DefaultIcon]

@="F:\\soft\\Tencent\\QQ\\Bin\\QQScLauncher.exe"

[HKEY_CLASSES_ROOT\myprotocol\shell]

@=""

[HKEY_CLASSES_ROOT\myprotocol\shell\open]

@=""

[HKEY_CLASSES_ROOT\myprotocol\shell\open\command]

@="\"F:\\soft\\Tencent\\QQ\\Bin\\QQScLauncher.exe\" "

双击注册表文件myprotocol.reg,进行注册

 选择“是”,之后选择确定

 通过如下方式打开注册表信息

可以看到注册成功了。

其次在网页中用js指向这个注册表文件,就可以实现网页调用本地应用程序 

创建QQ.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body style="text-align: center;padding-top: 300px;">
<button
οnclick="window.location.href='myprotocol://C:\\Users\\Administrator\\Desktop\\myprotocol.reg'"
style="margin: 200px auto;">打开QQ</button>
<a href="myprotocol://C:\Users\Administrator\Desktop\myprotocol.reg"
style="margin: 200px auto;">打开QQ</a>
</body>
</html>

双击用浏览器打开可以看到

 点击打开QQ按钮,会弹出如下所示弹窗

可以看到我们打开了本地的QQ软件

至此我们通过js成功打开了本地的程序。

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信