在工作中遇到过前端web页面调用本地应用的情况,一般情况下是不行的,后面了解前端通过注册表可以
之前IE有这个功能,谷歌没有,可以查看我之前这篇blog
大致代码如下,IE浏览器打开谷歌浏览器,但是谷歌浏览器没有这个方法
var objShell = new ActiveXObject("WScript.Shell")
// cmd调用Chrome打开当前网页
objShell.Run("cmd.exe /c start chrome " + window.location.href, 0, true)
下面是调用杏和用血的注册表的文件内容【svenTest.reg】:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\anesXh]
@="URL:anesXh Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\anesXh\DefaultIcon]
@="C:\\xhlis\\XingHeBrowser\\XHBrowserStartUp.exe"
[HKEY_CLASSES_ROOT\anesXh\shell]
[HKEY_CLASSES_ROOT\anesXh\shell\open]
[HKEY_CLASSES_ROOT\anesXh\shell\open\command]
@="cmd /c set m=%1 & call set m=%%m:anesXh:=%% & call set m=\"%%m:separator=&%%\" & call \"C:\\xhlis\\XingHeBrowser\\XHBrowserStartUp.exe\" %%m%% & exit"
下面是前端代码拼接方式:
// 代码调用拼接路径应该如下,但是发现参数总是拼接不上去
// http://IP地址:端口/Interface_Login.aspx?logid=用户工号&deptid=科室代码&usertype=用户类型&outpatientid=患者id
后面发现【&】连接符需要转换为【separator】,这个参考的是这个blog,代码最终如下:
const logid = '123123'
const deptId = '110'
const userType = 1
const patientid = '123456'
const url = `http://IP地址:端口/InterfaceLogin.aspx?logid=${logid}separatordeptid=${deptId}separatorusertype=${userType}separatoroutpatientid=${patientid}`
window.location.href = 'anesXh:' + url
当然也可以多个注册表写在一起,调用的时候区分别名就行:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\anesXh]
@="URL:anesXh Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\anesXh\DefaultIcon]
@="C:\\xhlis\\XingHeBrowser\\XHBrowserStartUp.exe"
[HKEY_CLASSES_ROOT\anesXh\shell]
[HKEY_CLASSES_ROOT\anesXh\shell\open]
[HKEY_CLASSES_ROOT\anesXh\shell\open\command]
@="cmd /c set m=%1 & call set m=%%m:anesXh:=%% & call set m=\"%%m:separator=&%%\" & call \"C:\\xhlis\\XingHeBrowser\\XHBrowserStartUp.exe\" %%m%% & exit"
[HKEY_CLASSES_ROOT\anesXhTwo]
@="URL:anesXhTwo Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\anesXhTwo\DefaultIcon]
@="C:\\xhlis\\XingHeBrowser-bg\\XHClickBrowser.exe"
[HKEY_CLASSES_ROOT\anesXhTwo\shell]
[HKEY_CLASSES_ROOT\anesXhTwo\shell\open]
[HKEY_CLASSES_ROOT\anesXhTwo\shell\open\command]
@="cmd /c set m=%1 & call set m=%%m:anesXhTwo:=%% & call set m=\"%%m:separator=&%%\" & call \"C:\\xhlis\\XingHeBrowser-bg\\XHClickBrowser.exe\" %%m%% & exit"
总结 – 需要注意以下几点:
1、首先需要知道调用程序的路径;
2、该注册表需要在调用端双击运行;
3、注册表调用别名和代码中要一致,示例用的是【anesXh】;
4、url多个参数拼接时,【&】连接符需要转换为【separator】