Web-based protocol handlers

  • Revision slug: Web-based_protocol_handlers
  • Revision title: Web-based protocol handlers
  • Revision id: 298364
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

Revision Content

{{ 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 applications 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/?uri=%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

Note:The URL template supplied when registering must be of the same domain as the webpage attempting to perform the registration or the registration will fail. For example, http://example.com/homepage.html can register a protocol handler for http://example.com/handle_mailto/%s, but not for http://example.org/handle_mailto/%s.

Example

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
  <title>Web Protocol Handler Sample - Register</title>
  <script type="text/javascript">
    navigator.registerProtocolHandler("fake", "http://starkravingfinkle.org/projects/wph/handler.php?value=%s", "Fake Protocol");
  </script>
</head>
<body>
  <h1>Web Protocol Handler Sample</h1>
  <p>This web page will install a web protocol handler for the <code>fake:</code> protocol.</p>
</body>
</html>

Activating

Now, anytime the user activates a link that uses the registered protocol, the browser will route the action to the URL supplied when the web application registered. Firefox will, by default, prompt the user before handing off the action.

Image:wph-launch.png

Example

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
  <title>Web Protocol Handler Sample - Test</title>
</head>
<body>
  <p>Hey have you seen <a href="fake:this%20is%20fake">this</a> before?</p>
</body>
</html>

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:

http://starkravingfinkle.org/projects/wph/handler.php?value=fake:this%20is%20fake

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

Note:The server side code is passed the entire contents of the href. This means the server side code will have to parse out the protocol from the data.

Example

<?php
$value = "";
if ( isset ( $_GET["value"] ) ) {
  $value = $_GET["value"];
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>Web Protocol Handler Sample</title>
</head>
<body>
  <h1>Web Protocol Handler Sample - Handler</h1>
  <p>This web page is called when handling a <code>fake:</code> protocol action. The data sent:</p>
  <textarea>
<?php echo(htmlspecialchars($valueENT_QUOTES, 'UTF-8')); ?>
  </textarea>
</body>
</html>

References

See also

{{HTML5ArticleTOC()}}

Revision Source

<div>{{ Fx_minversion_header(3) }}</div>
<h3 id="Background" 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>

<div class="overflow:hidden">
<pre class="brush:html">&lt;a href="mailto:webmaster@example.com"&gt;Web Master&lt;/a&gt;</pre>
</div>

<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 <em>desktop-based</em> 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 applications that could process a <code>mailto</code> link.</p>
<h3 id="Registering" name="Registering">Registering</h3>
<p>Setting up a web application as a protocol handler is not a difficult process. Basically, the web application uses <code><a href="/en/DOM/navigator.registerProtocolHandler" title="en/DOM/window.navigator.registerProtocolHandler">registerProtocolHandler()</a></code> to register itself with the browser as a potential handler for a given protocol. For example:</p>
<pre class="brush: js">
navigator.registerProtocolHandler("mailto",
                                  "https://www.example.com/?uri=%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" class="internal" src="/@api/deki/files/972/=Wph-notification.png" /></p>
<div class="note">
  <strong>Note:</strong>The URL template supplied when registering <strong>must</strong> be of the same domain as the webpage attempting to perform the registration or the registration will fail. For example, <code class="plain">http://example.com/homepage.html</code> can register a protocol handler for <code class="plain">http://example.com/handle_mailto/%s</code>, but not for <code class="plain">http://example.<em><strong>org</strong></em>/handle_mailto/%s</code>.</div>
<h4 id="Example" name="Example">Example</h4>
<pre class="brush: js">
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;title&gt;Web Protocol Handler Sample - Register&lt;/title&gt;
  &lt;script type="text/javascript"&gt;
    navigator.registerProtocolHandler("fake", "http://starkravingfinkle.org/projects/wph/handler.php?value=%s", "Fake Protocol");
  &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;h1&gt;Web Protocol Handler Sample&lt;/h1&gt;
  &lt;p&gt;This web page will install a web protocol handler for the &lt;code&gt;fake:&lt;/code&gt; protocol.&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<h3 id="Activating" name="Activating">Activating</h3>
<p>Now, anytime the user activates a link that uses the registered protocol, the browser will route the action to the 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" class="internal" src="/@api/deki/files/971/=Wph-launch.png" /></p>
<h4 id="Example_2" name="Example_2">Example</h4>
<pre class="brush: html">
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;title&gt;Web Protocol Handler Sample - Test&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;p&gt;Hey have you seen &lt;a href="fake:this%20is%20fake"&gt;this&lt;/a&gt; before?&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<h3 id="Handling" 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>
http://starkravingfinkle.org/projects/wph/handler.php?value=fake:this%20is%20fake
</pre>
<p>Server side code can extract the query string parameters and perform the desired action.</p>
<div class="note">
  <strong>Note:</strong>The server side code is passed the <strong>entire</strong> contents of the <code>href</code>. This means the server side code will have to parse out the protocol from the data.</div>
<h4 id="Example_3" name="Example_3">Example</h4>
<pre class="brush: php">
&lt;?php
$value = "";
if ( isset ( $_GET["value"] ) ) {
  $value = $_GET["value"];
}
?&gt;

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;title&gt;Web Protocol Handler Sample&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;h1&gt;Web Protocol Handler Sample - Handler&lt;/h1&gt;
  &lt;p&gt;This web page is called when handling a &lt;code&gt;fake:&lt;/code&gt; protocol action. The data sent:&lt;/p&gt;
  &lt;textarea&gt;
&lt;?php echo(<code><span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 187);">htmlspecialchars</span><span style="color: rgb(0, 119, 0);">(</span></span></code>$value<code><span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 119, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 187);">ENT_QUOTES</span><span style="color: rgb(0, 119, 0);">, 'UTF-8'));</span></span></code> ?&gt;
  &lt;/textarea&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<h3 id="References">References</h3>
<ul>
  <li><a class="external" href="http://www.w3.org/TR/html5/timers.html#custom-handlers" rel="freelink">http://www.w3.org/TR/html5/timers.ht...ustom-handlers</a></li>
</ul>

<h3 id="See_also">See also</h3>
<ul>
  <li><a href="/en/DOM/window.navigator.registerContentHandler" title="en/DOM/window.navigator.registerContentHandler">window.navigator.registerContentHandler</a></li>
  <li><a href="/en/XPCOM_Interface_Reference/nsIProtocolHandler" title="en/nsIProtocolHandler">nsIProtocolHandler</a> (XUL&nbsp;only)</li>
  <li><a class="external" href="http://blog.mozilla.com/webdev/2010/07/26/registerprotocolhandler-enhancing-the-federated-web/" title="http://blog.mozilla.com/webdev/2010/07/26/registerprotocolhandler-enhancing-the-federated-web/">RegisterProtocolHandler Enhancing the Federated Web</a> at Mozilla Webdev</li>
</ul>

<div>{{HTML5ArticleTOC()}}</div>
Revert to this revision