Sub topic 7
Email, Phone, and Download Links
This chapter assumes you can write an a element with an href, and that you know the difference between absolute and relative URLs. Links are not limited to web pages. The scheme at the start of the href value tells the browser which program or action should handle the link.
How the scheme decides the action
A URL scheme is the part before the first colon. https: tells the browser to fetch a page. mailto: and tel: hand the link to another application. The browser does not load anything itself.
Email and phone links
<a href="mailto:[email protected]">Email us</a>
<a href="tel:+15555550123">Call +1 555 555 0123</a>For mailto:, put the address right after the colon. You can add a subject with a query string, and spaces must be encoded as %20:
<a href="mailto:[email protected]?subject=Question%20about%20pricing">Ask about pricing</a>For tel:, write the number in international format, starting with + and the country code. Leave out spaces, dashes, and brackets in the href. The visible text can be formatted however you like. A number without a country code only works for callers in the same country.
Clicking tel: on a desktop computer may do nothing, or it may open a calling app. Because behavior varies, always show the address or number as the link text. A visitor can then copy it.
Download links
The download attribute asks the browser to save the file instead of opening it.
<a href="/files/guide.pdf" download>Download the guide (PDF)</a>You can give download a value to suggest a file name, as in download="setup-guide.pdf". Two limits apply:
- It only works for same-origin URLs, meaning files on your own site. For a file on another site, the browser ignores the attribute and navigates to the file.
- The server can override it. A
Content-Dispositionresponse header takes priority over the attribute.
Common errors
- Putting a space or a dash inside a
tel:value, or leaving out the+and country code. - Forgetting to encode spaces in a
mailto:subject. - Showing only "Click here" as the link text. Write what the link does, such as "Email us" or "Download the guide (PDF)".
- Leaving out the file type and size on a download link. Visitors should know that a click will save a PDF.
Exercise
Write a link that lets visitors call +44 20 7946 0000, and a link that downloads the file /files/menu.pdf. What will the browser do with the download link if the file is on a different website?
Show answer
<a href="tel:+442079460000">Call +44 20 7946 0000</a> and <a href="/files/menu.pdf" download>Download the menu (PDF)</a>. The tel value has no spaces and starts with + and the country code. If the file is on a different website, the browser ignores the download attribute and navigates to the file, because download only works for same-origin URLs.