It's fairly common to find web pages link to resources using non-
http protocols. An example is the
<a href="mailto:email@example.com">Web Master</a>
Web authors can use a
mailto: link when they want to provide a convenient way for users to send an email, directly from the webpage. When the link is activated, the browser should launch the default desktop application for handling email. You can think of this as a desktop-based protocol handler.
Web-based protocol handlers allow web-based applications to participate in the process too. This is becoming more important as more types of applications migrate to the web. In fact, there are many web-based email handling application that could process a
Setting up a web application as a protocol handler is not a difficult process. Basically, the web application uses registerProtocolHandler to register itself with the browser as a potential handler for a given protocol. For example:
navigator.registerProtocolHandler("mailto", "https://www.example.com/?to=%s", "Example Mail");
Where the parameters are:
- The protocol.
- The URL template, used as the handler. The "%s" is replaced with the
hrefof the link and a GET is executed on the resultant URL.
- The user friendly name for the protocol handler.
When a browser executes this code, it should display a prompt to the user, asking permission to allow the web application to register as a handler for the protocol. Firefox displays a prompt in the notification bar area:
Now, anytime the user activates a link that uses the registered protocol, the browser will route the action to URL supplied when the web application registered. Firefox will, by default, prompt the user before handing off the action.
The next phase is handling the action. The browser extracts the
href from the activated link, combines it with the URL template supplied during handler registration and performs an HTTP GET on the URL. So, using the above examples, the browser would perform a GET on this URL:
Server side code can extract the query string parameters and perform the desired action.