再一次,又是因为工作上的需要,希望能在网页上启动用户端的应用程序。其实这也不是新闻啦!因为之前就写过了,只是之前几乎都是使用ActiveX。但现在使用ActiveX呢,要设一堆安全性,还可能要签署(有需要可以参考
使用经济部工商凭证签署ActiveX控件),真是麻烦到爆。若是用户只有少数或Intranet或有区域性那都还好,若要给普罗大众用,这样的做法真的不太优雅,更遑论IE以外的浏览器都不能用。
点了View In ITunes后,Google Chrome会出现如下的画面
而IE会出现
FireFox
Safari好样的,根本连提示讯息都没有,就直接开启应用程序了
总之,就是有跨浏览器且更优雅的方式,OK!Let's do it!
如果不想看Microsoft官方文件的,就往下看吧!做法其实相当简单,就是要到Registry里注册一个URL Protocol,然后加入一些key跟subkey,不过看文字看得眼睛都花了,我们不如开启登录编辑器,看看ITunes里的itms:是如何定义的
保存为test.reg
Windows Registry Editor Version 5.00
@="URL:iTunes Store Protocol"
"AppUserModelID"="Apple.iTunes"
[HKEY_CLASSES_ROOT\itms\DefaultIcon]
@="C:\\Program Files (x86)\\iTunes\\iTunes.exe"
[HKEY_CLASSES_ROOT\itms\shell]
[HKEY_CLASSES_ROOT\itms\shell\open]
[HKEY_CLASSES_ROOT\itms\shell\open\command]
@="C:\\Program Files (x86)\\iTunes\\iTunes.exe /url \"%1\""
蓝色的部分,是这个Protocol的描述,您可以改为自己的描述
红色的部分,是要启动应用程序所在路径(第一个其实是IE对话窗口里左上角那个图标所在位置,若您懂得我在说甚么,您可以自定义为其他位置)
绿色的部分,后面感觉是对到下面的Key,但是我删掉后好像也没甚么影响,就先不理,删了它吧
紫色的部分,就是Protocol的名字,以下的例子使用的Protocol为kai
改完之后结果像这样
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\kai\DefaultIcon]
@="D:\\Kai\\bin\\Debug\\Kai.exe"
[HKEY_CLASSES_ROOT\kai\shell]
[HKEY_CLASSES_ROOT\kai\shell\open]
[HKEY_CLASSES_ROOT\kai\shell\open\command]
@="D:\\Kai\\bin\\Debug\\Kai.exe /url \"%1\""
存档为*.reg,接着Double Click存档后的*.reg,按"是"
这样就注册完成一个新的Protocol,再检视一次看看
接下来撰写Kai.exe
内容如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Kai
{
class Program
{
static void Main(string[] args)
{
string ss = "";
foreach (string s in args)
{
ss += "\n" + s;
}
Console.Write(ss);
Console.Read();
}
}
}
接下来让我们做一个html档,内容如下
<html>
<body>
<div></div>
<a href="kai://somethingforexe">测试一下</a>
</body>
</html>
使用IE开启这个html档并点选超链接
传递进来的参数也收到了,接下来,一样自行发挥啰
成功! :)
2013-1-15Custom URL Protocol
- About Asynchronous Pluggable Protocols
- URI scheme: Every URI is defined as consisting of four parts, as follows:
- <scheme name> : <hierarchical part> [ ? <query> ] [ # <fragment> ]
- The scheme name consists of a sequence of characters beginning with a letter and followed by any combination of letters, digits, plus ("+"), period ("."), or hyphen ("-"). Although schemes are case-insensitive, the canonical form is lowercase and documents that specify schemes must do so with lowercase letters. It is followed by a colon (":").
- Custom URL Scheme maximum URL length
评论