前端通过js唤醒本地安装exe程序
网上有很多方法,大部分只能在ie上用。编写完reg 文件 然后双击安装,在运行html 进行测试即可。openExe.bat (具体调用exe的文件)通用方法(支持IE/火狐/谷歌)URL协议 来实现的。
·
前提
在web项目中,经常遇到需要在某个页面上需要调用本地的程序exe,网上有很多方法,大部分只能在ie上用 。
百度的结果
ActiveXObject插件是IE所特有的,在不同浏览器内核下是无法通用的。只有用ie浏览器才可以使用ActiveXObject。
在浏览网页中发现 有个 a标签中 mailto:
用法,直接可以调用发送邮件方法,腾讯在很多地方可以调用起qq进行聊天,这个功能就非常好,体验也不错,研究了一下他们的实现,发现是通过自定义windows
URL协议 来实现的。
通用方法(支持IE/火狐/谷歌)
openExe.bat (具体调用exe的文件)
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\openExe]
@="URL:openExe Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\openExe\DefaultIcon]
@="openExe.exe,1"
[HKEY_CLASSES_ROOT\openExe\shell]
[HKEY_CLASSES_ROOT\openExe\shell\open]
[HKEY_CLASSES_ROOT\openExe\shell\open\command]
@="cmd /v /c \"set URL=%1 && set URL=!URL:openExe=http!&&cmd /c start chrome !URL!\""
openExe 自定义协议名
start chrome 运行的 exe文件
实例html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<div>
<a href="openExe:www.baidu.com">执行可执行文件(协议名:参数)</a>
</div>
</body>
</html>
测试
编写完reg 文件 然后双击安装,在运行html 进行测试即可。
拓展
protocolcheck.js
浏览器可以调起客户端本地程序的方法,在企业环境的实际应用中,又面临着如何检测自定义的协议是否存在的问题,ie下可以通过activex检测注册表项,chrome该怎么办呢?有没有通用的解决办法呢?答案是肯定的,甚至有人已经将该功能封装成了通用JS。具体如下。
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.protocolCheck = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
function _registerEvent(target, eventType, cb) {
if (target.addEventListener) {
target.addEventListener(eventType, cb);
return {
remove: function () {
target.removeEventListener(eventType, cb);
}
};
} else {
target.attachEvent(eventType, cb);
return {
remove: function () {
target.detachEvent(eventType, cb);
}
};
}
}
function _createHiddenIframe(target, uri) {
var iframe = document.createElement("iframe");
iframe.src = uri;
iframe.id = "hiddenIframe";
iframe.style.display = "none";
target.appendChild(iframe);
return iframe;
}
function openUriWithHiddenFrame(uri, failCb, successCb) {
var timeout = setTimeout(function () {
failCb();
handler.remove();
}, 1000);
var iframe = document.querySelector("#hiddenIframe");
if (!iframe) {
iframe = _createHiddenIframe(document.body, "about:blank");
}
var handler = _registerEvent(window, "blur", onBlur);
function onBlur() {
clearTimeout(timeout);
handler.remove();
successCb();
}
iframe.contentWindow.location.href = uri;
}
function openUriWithTimeoutHack(uri, failCb, successCb) {
var timeout = setTimeout(function () {
failCb();
handler.remove();
}, 1000);
//handle page running in an iframe (blur must be registered with top level window)
var target = window;
while (target != target.parent) {
target = target.parent;
}
var handler = _registerEvent(target, "blur", onBlur);
function onBlur() {
clearTimeout(timeout);
handler.remove();
successCb();
}
window.location = uri;
}
function openUriUsingFirefox(uri, failCb, successCb) {
var iframe = document.querySelector("#hiddenIframe");
if (!iframe) {
iframe = _createHiddenIframe(document.body, "about:blank");
}
try {
iframe.contentWindow.location.href = uri;
successCb();
} catch (e) {
if (e.name == "NS_ERROR_UNKNOWN_PROTOCOL") {
failCb();
}
}
}
function openUriUsingIEInOlderWindows(uri, failCb, successCb) {
if (getInternetExplorerVersion() === 10) {
openUriUsingIE10InWindows7(uri, failCb, successCb);
} else if (getInternetExplorerVersion() === 9 || getInternetExplorerVersion() === 11) {
openUriWithHiddenFrame(uri, failCb, successCb);
} else {
openUriInNewWindowHack(uri, failCb, successCb);
}
}
function openUriUsingIE10InWindows7(uri, failCb, successCb) {
var timeout = setTimeout(failCb, 1000);
window.addEventListener("blur", function () {
clearTimeout(timeout);
successCb();
});
var iframe = document.querySelector("#hiddenIframe");
if (!iframe) {
iframe = _createHiddenIframe(document.body, "about:blank");
}
try {
iframe.contentWindow.location.href = uri;
} catch (e) {
failCb();
clearTimeout(timeout);
}
}
function openUriInNewWindowHack(uri, failCb, successCb) {
var myWindow = window.open('', '', 'width=0,height=0');
myWindow.document.write("<iframe src='" + uri + "'></iframe>");
setTimeout(function () {
try {
myWindow.location.href;
myWindow.setTimeout("window.close()", 1000);
successCb();
} catch (e) {
myWindow.close();
failCb();
}
}, 1000);
}
function openUriWithMsLaunchUri(uri, failCb, successCb) {
navigator.msLaunchUri(uri,
successCb,
failCb
);
}
function checkBrowser() {
var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
var ua = navigator.userAgent.toLowerCase();
return {
isOpera : isOpera,
isFirefox : typeof InstallTrigger !== 'undefined',
isSafari : (~ua.indexOf('safari') && !~ua.indexOf('chrome')) || Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0,
isIOS : /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream,
isChrome : !!window.chrome && !isOpera,
isIE : /*@cc_on!@*/false || !!document.documentMode // At least IE6
}
}
function getInternetExplorerVersion() {
var rv = -1;
if (navigator.appName === "Microsoft Internet Explorer") {
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat(RegExp.$1);
}
else if (navigator.appName === "Netscape") {
var ua = navigator.userAgent;
var re = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null) {
rv = parseFloat(RegExp.$1);
}
}
return rv;
}
module.exports = function(uri, failCb, successCb, unsupportedCb) {
function failCallback() {
failCb && failCb();
}
function successCallback() {
successCb && successCb();
}
if (navigator.msLaunchUri) { //for IE and Edge in Win 8 and Win 10
openUriWithMsLaunchUri(uri, failCb, successCb);
} else {
var browser = checkBrowser();
if (browser.isFirefox) {
openUriUsingFirefox(uri, failCallback, successCallback);
} else if (browser.isChrome || browser.isIOS) {
openUriWithTimeoutHack(uri, failCallback, successCallback);
} else if (browser.isIE) {
openUriUsingIEInOlderWindows(uri, failCallback, successCallback);
} else if (browser.isSafari) {
openUriWithHiddenFrame(uri, failCallback, successCallback);
} else {
unsupportedCb();
//not supported, implement please
}
}
}
},{}]},{},[1])(1)
});
具体使用
html 文件引入 js,即可
document.getElementById("xieyi").onclick = function () {
window.protocolCheck('自定义协议',
function () {
alert("协议未注册");
});
event.preventDefault ? event.preventDefault() : event.returnValue = false;
}
更多推荐
已为社区贡献3条内容
所有评论(0)