The Navigator
method registerProtocolHandler()
lets websites register their ability to open or handle particular URL schemes (aka protocols).
For example, this API lets webmail sites open mailto:
URLs, or VoIP sites open tel:
URLs.
registerProtocolHandler(scheme, url)
For security reasons, registerProtocolHandler()
restricts which schemes can be registered.
A custom scheme may be registered as long as:
- The custom scheme's name begins with
web+
- The custom scheme's name includes at least 1 letter after the
web+
prefix - The custom scheme has only lowercase ASCII letters in its name.
For example, web+burger
, as shown in the Example below.
Otherwise, the scheme must be one of the following:
bitcoin
ftp
ftps
geo
im
irc
ircs
magnet
mailto
matrix
mms
news
nntp
openpgp4fpr
sftp
sip
sms
smsto
ssh
tel
urn
webcal
wtai
xmpp
If your site is burgers.example.com
, you can register a protocol handler for it to handle web+burger:
links, like so:
navigator.registerProtocolHandler(
"web+burger",
"https://burgers.example.com/?burger=%s",
);
This creates a handler that lets web+burger:
links send the user to your site, inserting the accessed burger URL into the %s
placeholder.
This script must be run from the same origin as the handler URL (so any page at https://burgers.example.com
), and the handler URL must be http
or https
.
The user will be notified that your code asked to register the protocol handler, so that they can decide whether or not to allow it. See the screenshot below for an example on google.co.uk
: