Sub topic 7
Email, phone, and download links
This chapter assumes you can write an a element with an href, and that you know how absolute, root-relative, and relative paths differ. Here you will use URL schemes other than http and https to start an email, a phone call, or a file download. You will also see where each one fails and how to write link text for them.
How the browser reads an href
The text before the first colon in an href is the URL scheme. For https: the browser fetches a page. For other schemes it passes the link to another program.
If nothing on the device handles the scheme, the click does nothing, or the browser shows a prompt. Your page cannot detect this, so the link text should always give the user a way to finish the task without the link.
Email links with mailto
A mailto: link opens the user's default email program with a new message.
<a href="mailto:[email protected]">[email protected]</a>Show the address as the link text. Someone on a computer with no email program configured can still copy the address.
Adding recipients, subject, and body
After the address, add a query string that starts with ?. Separate further fields with &.
<a href="mailto:[email protected]?subject=Order%20question&body=Hello%2C%0D%0A%0D%0AMy%20order%20number%20is%3A%20">
Email support about an order
</a>The fields you can use are:
| Field | Purpose | Example |
|---|---|---|
| `subject` | Subject line | `subject=Order%20question` |
| `body` | Message text | `body=Hello%2C` |
| `cc` | Copied recipients | `[email protected]` |
| `bcc` | Hidden recipients | `[email protected]` |
Several recipients go in the address part, separated by commas: mailto:[email protected],[email protected].
Percent-encoding
Spaces, line breaks, and characters such as &, ?, #, and , have special meaning in a URL. Replace them with a percent sign and a hex code:
- A space is
%20. - A line break is
%0D%0A. - A comma is
%2C. - An ampersand inside text is
%26.
In the HTML source, write the & that separates fields as &. Browsers accept a bare &, but & is the valid form.
Recipients, subject, and body are only suggestions. The user can change them before sending, and some email apps ignore body or limit its length. Keep prefilled text short.
Phone links with tel
A tel: link asks the device to start a call. Phones and many desktop apps (such as softphones and video-call software) handle it.
<a href="tel:+15555550123">Call +1 555 555 0123</a>Follow these rules:
- Use the international format:
+, the country code, then the number. - Leave spaces, dashes, and parentheses out of the
href. They are legal, but the plain digits are the most dependable form. - Show the number in the link text in a readable format.
- For extensions, add pause characters:
tel:+15555550123,,42waits, then dials 42. Support varies by device.
The sms: scheme works the same way for text messages, for example sms:+15555550123. Support for prefilling the message text differs between operating systems, so treat the text as optional.
On a desktop browser with no calling software, a tel: link may do nothing. If most of your visitors are on desktops, display the number as plain text next to the link.
Download links
The download attribute tells the browser to save the target file instead of opening it. It is a boolean attribute, so it needs no value.
<a href="/files/guide.pdf" download>Download the guide (PDF)</a>Give it a value to suggest a filename:
<a href="/files/report-2024-q4.pdf" download="quarterly-report.pdf">Download the Q4 report (PDF, 1.2 MB)</a>Limits you will run into
- Same-origin only. The attribute is honored for files from your own origin (same scheme, host, and port), and for
blob:anddata:URLs. For a file on another domain, browsers ignore it and navigate to the file. - The server can override it. A
Content-Dispositionresponse header takes priority over the markup. If the header saysinline, some browsers may still open the file. If it saysattachment, the file downloads even without the attribute. - The filename is a suggestion. The browser may change it, for example to remove unsafe characters.
- Without the attribute, a PDF or image usually opens in the browser. Use
downloadonly when saving is what you want. For a document people may want to read first, a plain link is often kinder.
A complete page that uses all three link types:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Contact and downloads</title>
</head>
<body>
<h1>Contact us</h1>
<ul>
<li><a href="mailto:[email protected]?subject=Website%20enquiry">Email [email protected]</a></li>
<li><a href="tel:+15555550123">Call +1 555 555 0123</a></li>
<li><a href="files/guide.pdf" download>Download the guide (PDF, 1.2 MB)</a></li>
</ul>
</body>
</html>To test the download, put a real guide.pdf in a files folder next to the page. Open the page through a local web server, or directly from disk. Browsers differ in how they treat the download attribute on file: URLs.
Writing accessible link text
These links do something unexpected: they leave the browser, or they save a file. The link text should say what will happen.
- Name the action: "Email support", "Call the sales team", "Download the price list".
- Give the file type and size for downloads, such as "(PDF, 1.2 MB)". Users on slow or metered connections need this.
- Avoid "click here". A screen reader user listing the links on a page hears each link out of context.
- Put the visible number or address in the text. Voice-control users can then say the text they see.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| `href="tel:555 0123"` | No country code, so the call fails abroad | `tel:+15555550123` |
| Spaces in `subject=` | Some mail apps cut the subject at the first space | Use `%20` |
| `download` on a link to another domain | The browser opens the file instead of saving it | Host the file on your origin, or set `Content-Disposition` on the server |
| `target="_blank"` on a mailto link | Can leave an empty tab behind in some browsers | Leave `target` off |
| Link text "Click here" | Meaningless out of context | Describe the action |
Exercise
A link reads <a href="https://cdn.example.net/docs/manual.pdf" download>Get the manual</a> on a page served from www.example.com. A user clicks it and the PDF opens in the browser tab instead of saving. Why, and what are two ways to fix it?
Show answer
The file is on a different origin (cdn.example.net), so browsers ignore the download attribute. Fix it by hosting the file on www.example.com so the link is same-origin, or by making the CDN send a Content-Disposition: attachment header for that file. Improving the link text to "Download the manual (PDF)" also helps users.
Exercise
Write an anchor that opens a new email to [email protected] with the subject "Quote request" and a copy to [email protected].
Show answer
<a href="mailto:[email protected]?subject=Quote%20request&[email protected]">Email sales for a quote</a>