Html 如何使用超链接运行外部程序,例如记事本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2800081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run an external program, e.g. notepad, using hyperlink?
提问by Nam G VU
I'm generating an HTML report by C# to print pairs of files in a table which has 3 columns: the first two columns used for the filenames and the 3rd column is a hyperlink Compare - I want this link to run WinMerge to compare to two files and I currently don't know how to do it.
我正在通过 C# 生成一个 HTML 报告,以在具有 3 列的表中打印文件对:前两列用于文件名,第三列是超链接比较 - 我希望此链接运行 WinMerge 以与两列进行比较文件,我目前不知道该怎么做。
采纳答案by Nam G VU
Try this
尝试这个
<html>
<head>
<script type="text/javascript">
function runProgram()
{
var shell = new ActiveXObject("WScript.Shell");
var appWinMerge = "\"C:\Program Files\WinMerge\WinMergeU.exe\" /e /s /u /wl /wr /maximize";
var fileLeft = "\"D:\Path\to\your\file\"";
var fileRight= "\"D:\Path\to\your\file2\"";
shell.Run(appWinMerge + " " + fileLeft + " " + fileRight);
}
</script>
</head>
<body>
<a href="javascript:runProgram()">Run program</a>
</body>
</html>
回答by Nick Craver
Sorry this answer sucks, but you can't launch an just any external application via a click, as this would be a serious security issue, this functionality isn't available in HTML or javascript. Think of just launching cmd.exe
with args...you want to launch WinMerge with arguments, but you can see the security problems introduced by allowing this for anything.
抱歉,这个答案很糟糕,但您不能通过单击启动任何外部应用程序,因为这将是一个严重的安全问题,此功能在 HTML 或 javascript 中不可用。想想只cmd.exe
用 args启动……你想用参数启动 WinMerge,但你可以看到允许它用于任何事情所引入的安全问题。
The only possiblyviable exception I can think of would be a protocol handler (since these are explicitlydefined handlers), like winmerge://
, though the best way to pass 2 file parameters I'm not sure of, if it's an option it's worth looking into, but I'm not sure what you are or are not allowed to do to the client, so this may be a non-starter solution.
我能想到的唯一可能可行的例外是协议处理程序(因为这些是明确定义的处理程序),例如winmerge://
,尽管我不确定传递 2 个文件参数的最佳方式,如果这是一个值得研究的选项,但我不确定您可以或不允许对客户做什么,因此这可能是一个非入门解决方案。
回答by Jakub Hampl
The reasonable way how to launch apps from HTML is through url schemes. So you can launch email via mailto:
links and irc through irc:
links. Individual apps can implement these schemes, but I'm not sure WinMerge does this.
如何从 HTML 启动应用程序的合理方法是通过 url 方案。因此,您可以通过mailto:
链接启动电子邮件,通过链接启动irc irc:
。单个应用程序可以实现这些方案,但我不确定 WinMerge 是否这样做。
回答by Sheryar Nizar
Make a batch file and call the bacth file in Window.open. Here how it works
制作一个批处理文件并在Window.open 中调用该bacth 文件。这是它是如何工作的
- make a file in notepad
- write your script : start wmplayer "\dotnet\sc\1234.mp4" /fullscreen
- save as : test.bat in \dotnet\sc\test.bat
- 在记事本中创建一个文件
- 编写脚本:启动 wmplayer "\dotnet\sc\1234.mp4" /fullscreen
- 另存为:\dotnet\sc\test.bat 中的 test.bat
in html
在 html 中
window.open('file://dotnet/sc/test.bat')
window.open('file://dotnet/sc/test.bat')
Enjoy..
享受..
回答by Felix D.
I've wrote a small extension to do so.
我写了一个小的扩展来做到这一点。
Since you are creating the page using C# you may want to implement this:
由于您使用 C# 创建页面,因此您可能需要实现:
https://github.com/felix-d-git/DesktopAppLink
https://github.com/felix-d-git/DesktopAppLink
Basically u are creating some registry entries to parse the links you click in your html page.
基本上,您正在创建一些注册表项来解析您在 html 页面中单击的链接。
The browser will then ask to open the specified app.
然后浏览器会要求打开指定的应用程序。
C#:
C#:
DesktopAppLink.CreateLink("applink.sample", "\"<path to exe>\"", "");
HTML:
HTML:
<a href="applink.sample:">Run Desktop App</a>
Result:
结果: