Revision 72326 of Web-based protocol handlers

  • Revision slug: Web-based_protocol_handlers
  • Revision title: Web-based protocol handlers
  • Revision id: 72326
  • Created:
  • Creator: MarkFinkle
  • Is current revision? No
  • Comment Initial overview of web protocol handlers

Revision Content

{{template.Fx_minversion_header(3)}}

Background

It's fairly common to find web pages link to resources using non-http protocols. An example is the mailto: protocol:

<a href="mailto:webmaster@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 mailto link.

Registering

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 href of 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:

Image:wph-notification.png

Activating

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.

Image:wph-launch.png

Handling

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:

https://www.example.com/?to=mailto:webmaster@example.com

Server side code can extract the query string parameters and perform the desired action.

Revision Source

<p>
{{template.Fx_minversion_header(3)}}
</p>
<h3 name="Background">Background</h3>
<p>It's fairly common to find web pages link to resources using non-<code>http</code> protocols. An example is the <code>mailto:</code> protocol:
</p>
<pre>&lt;a href="mailto:webmaster@example.com"&gt;Web Master&lt;/a&gt;
</pre>
<p>Web authors can use a <code>mailto:</code> 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 <i>desktop-based</i> protocol handler.
</p><p>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 <code>mailto</code> link.
</p>
<h3 name="Registering">Registering</h3>
<p>Setting up a web application as a protocol handler is not a difficult process. Basically, the web application uses <a href="en/DOM/window.navigator.registerProtocolHandler">registerProtocolHandler</a> to register itself with the browser as a potential handler for a given protocol. For example:
</p>
<pre>navigator.registerProtocolHandler("mailto",
                                  "https://www.example.com/?to=%s",
                                  "Example Mail");
</pre>
<p>Where the parameters are:
</p>
<ul><li> The protocol.
</li><li> The URL template, used as the handler. The "%s" is replaced with the <code>href</code> of the link and a GET is executed on the resultant URL.
</li><li> The user friendly name for the protocol handler.
</li></ul>
<p>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:
</p><p><img alt="Image:wph-notification.png" src="File:en/Media_Gallery/Wph-notification.png">
</p>
<h3 name="Activating">Activating</h3>
<p>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.
</p><p><img alt="Image:wph-launch.png" src="File:en/Media_Gallery/Wph-launch.png">
</p>
<h3 name="Handling">Handling</h3>
<p>The next phase is handling the action. The browser extracts the <code>href</code> 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:
</p>
<pre>https://www.example.com/?to=mailto:webmaster@example.com
</pre>
<p>Server side code can extract the query string parameters and perform the desired action.
</p>
Revert to this revision