对于 Chrome (13+)、Firefox (3.0+) 和 Opera (11.60+),可以使用 JavaScript API 将 Web 应用程序注册为自定义 URI 方案的服务处理程序:
window.navigator.registerProtocolHandler(protocol, uri, title);
-
protocol
是站点希望处理的协议,指定为字符串。
-
uri
是处理程序的字符串形式的 URI。您可以包含“%s”来指示插入要处理的文档的转义 URI 的位置。
-
title
是以字符串形式呈现给用户的处理程序的标题。
特别是对于 Chrome 有一个限制,不允许使用不以web+
前缀(标准前缀除外:mailto
, mms
, nntp
, rtsp
and webcal
)。因此,如果您想像 GMail 一样将您的 Web 应用程序注册为服务处理程序,您应该编写如下内容:
navigator.registerProtocolHandler("mailto", "https://www.example.com/?uri=%s", "Example Mail");
or
navigator.registerProtocolHandler("web+myscheme", "https://www.example.com/?uri=%s", "My Cool App");
注意 URI 模式,它必须包含%s
它将替换为用户点击的链接的实际 URI。例如:
<a href="web+myscheme:some+data">Open in "My Cool App"</a>
将触发GET
请求http://www.example.com/?uri=web%2Bmyscheme%3Asome%20data
以下是一些有用的链接:
- 标准http://www.whatwg.org/specs/web-apps/current-work/#custom-handlers http://www.whatwg.org/specs/web-apps/current-work/#custom-handlers
- MDN https://developer.mozilla.org/en-US/docs/DOM/navigator.registerProtocolHandler https://developer.mozilla.org/en-US/docs/DOM/navigator.registerProtocolHandler
- HTML5摇滚http://updates.html5rocks.com/2011/06/Registering-a-custom-protocol-handler http://updates.html5rocks.com/2011/06/Registering-a-custom-protocol-handler