New HTML5 Attributes for Hyperlinks: download, media, and ping

Share this article

Hyperlinks have been around since the dawn of the Web. But with the dawn of HTML5, three new attributes have been added to the humble <a> tag to keep existing attributes like href, rel, and others company.

The new attributes are: download, media, and ping. In this article, we’ll take a quick look at what these new attributes are, and how they can be used once browser support improves.

The download Attribute

The download attribute is new in HTML5. It supplements the existing href attribute by telling the browser that the resource the href points to should be downloaded directly, instead of visited (which could happen with a file that the browser can open, like a PDF). The value of the download attribute is used for the name of the file that is downloaded.

The download attribute can be the same as the file referenced in the href attribute, but it doesn’t have to be. Being able to have different values for href and download can come in handy. For example, you might need to generate unique files dynamically on the server for monthly or yearly reports on a per user basis, but still present the user with a meaningful filename for when they download a file. Because the download attribute can be different to the href, this is pretty easy to do:

<a href="files/eid987jdien2i.pdf"
   download="Monthly Report for March 2014.pdf">Download March 2014 Report</a>

When a user clicks the download link, they’ll download Monthly Report for March 2014.pdf rather than the endearingly named eid987jdien2i.pdf.

In theory there aren’t any restrictions on what you can enter for the download attribute. In practice this isn’t quite true, as you’ll need to bear in mind the restrictions operating systems impose on which characters can’t be used in filenames — such as the backslash ‘\’ on Windows, or the forward slash ‘/’ on *nix and OS X — and that the browser may adjust the download attribute’s value accordingly. It’s also worth noting that the download attribute’s value can be overridden by the Content-Disposition HTTP header’s filename parameter.

The download attribute can be used with blob and data URI’s, which makes it useful where users need to be able to save dynamic content they’ve created through your web application (e.g. drawing applications).

Typically you would set the href attribute to the blob or data URI, then, as with the monthly report example earlier, set the download attribute to a more meaningful file name. The following example shows how you can do this when using the Canvas API.

Here is our HTML:

<section>
    <canvas id="c" width="400" height="400"></canvas>
    <footer>
        <a id="download-canvas" href="#">Download</a>
    </footer>
</section>

And the JavaScript:

(function() {
    var canvas = document.getElementById('c'),
        cxt = canvas.getContext('2d'),
        downloadLink = document.getElementById('download-canvas');

    cxt.fillRect(100, 100, 200, 200);
    cxt.clearRect(150, 150, 100, 100);

    downloadLink.href = canvas.toDataURL();
    downloadLink.download = "squares.png";
})();

And here’s a live demo:

JS Bin

Note that in a real-world application, it is likely that the download attribute would not be hard coded, but would instead be added after input by the user ahead of downloading the image.

Browser Support for the download Attribute

While you may be thinking that the download attribute heralds the end of you having to implement file download logic on the server, unfortunately it isn’t fully supported across all major browsers. At the time of writing, Safari and IE (both desktop and mobile) do not support this attribute.

The media Attribute

If you’ve worked with CSS, then you’ve come across the media attribute before, and you’ve probably used it on the link tag. In HTML5 the media attribute can now also be applied to a hyperlink. It works in the same way, and its value can be any valid media query.

When applied to the <a> tag, the media attribute is used in a purely advisory capacity. It could be useful, for example, in situations where you provide device-specific downloads, such as for desktop and mobile wallpapers:

<ul>
    <li>
        <a href="download/320" media="min-width: 320px">
            <img src="files/320.jpg" alt="">
        </a>
    </li>
    <li>
        <a href="download/1382" media="min-width: 1382px">
            <img src="files/1382.jpg" alt="">
        </a>
    </li>
</ul>

It could also be applied to those ‘Print’ links you often see on long, multi-page articles, where you follow the link to get the whole article on one page formatted for printing:

<footer>
    <p>Page 1 of 6 <a href="/print/1234" media="print">Print All</a></p>
</footer>

Browser Support for the media Attribute

As of this writing, it doesn’t seem that there is much support, if any, for this attribute on hyperlinks. It’s listed as an HTML5 attribute on MDN’s HTML reference but it’s not listed as a valid attribute on the WHATWG spec or on W3C.

The ping Attribute

Finally, let’s look at the new ping attribute. This attribute takes a space-separated list of URL’s that are to be pinged should the user successfully navigate to the href of the hyperlink. Or, to put it another way, it provides native support for click and link tracking:

<ul>
    <li>
        <a href="/products/blaster" ping="/track/products/blaster">Blasters</a>
    </li>
    <li>
        <a href="/products/light-sabres" ping="/track/products/light-sabres">Light sabres (choice of colours)</a>
    </li>
</ul>

However, there’s a word of warning from the WHATWG spec:

The ping attribute is redundant with pre-existing technologies like HTTP redirects and JavaScript in allowing Web pages to track which off-site links are most popular or allowing advertisers to track click-through rates.

So, if the ping attribute is redundant with pre-existing technologies, what’s the point of it? Well, the idea is that it gives users greater transparency in seeing what other requests may be made as a result of them clicking on a certain link. It’s this greater transparency that is why authors are being encouraged to use this attribute.

It’s been suggested that browsers should allow users to configure how ping notifications are handled e.g. by blocking some URL’s, only allow same origin URL’s, or disabling ping altogether. Browsers could also display the ping URL alongside the link location in the browser’s status bar.

Browser Support for the ping Attribute

Current browser support for ping is mixed. Safari and Chrome do support it, Firefox has support for it but by default it is disabled, and IE and Opera do not support it. It’s also worth noting that the ping attribute is not in the current W3C HTML5 draft spec, but it is in the WHATWG HTML5 spec.

Summary

So that’s 3 new attributes that were added to the <a> element in HTML5 — download, media, and ping.

As you can see, when these gain better browser support, they will be quite useful and have lots of potential use cases.

If you can think of any unique ways these can be used, we’d love to hear your thoughts in the comments.

What is the purpose of the ‘download’ attribute in HTML5?

The ‘download’ attribute in HTML5 is used to instruct the browser to download the linked resource rather than navigating to it. This attribute can be particularly useful when you want the user to download a file, such as a PDF or a document, directly from your webpage. It’s important to note that the ‘download’ attribute only works for same-origin URLs, or URLs that use the ‘blob:’ or ‘data:’ schemes.

How does the ‘media’ attribute work in HTML5?

The ‘media’ attribute in HTML5 is used to specify what media or device the linked resource is optimized for. This attribute accepts media queries, which allow you to target specific device characteristics. For example, you could use the ‘media’ attribute to link to a stylesheet that is only applied when the user is on a screen that is 600px wide or smaller.

Why isn’t the ‘download’ attribute working in my Chrome browser?

There could be a few reasons why the ‘download’ attribute isn’t working in your Chrome browser. One common issue is that the ‘download’ attribute only works for same-origin URLs. If you’re trying to use the ‘download’ attribute with a cross-origin URL, it won’t work. Another potential issue could be related to the file type. Some file types, like .html or .txt, might open in a new tab instead of downloading.

What is the ‘href’ attribute in HTML5?

The ‘href’ attribute in HTML5 is used to specify the URL of the page the link goes to. This attribute can be used with most elements, but it’s most commonly used with the ‘a’ and ‘link’ elements. The ‘href’ attribute can also be used to link to sections within a webpage by using the ID of the element you want to link to as the value.

Why isn’t the ‘download’ attribute working as expected?

The ‘download’ attribute might not work as expected for a few reasons. One common issue is that the ‘download’ attribute only works for same-origin URLs. If you’re trying to use the ‘download’ attribute with a cross-origin URL, it won’t work. Another potential issue could be related to the file type. Some file types, like .html or .txt, might open in a new tab instead of downloading. Additionally, some browsers do not support the ‘download’ attribute, which could also cause it to not work as expected.

How can I use the ‘ping’ attribute in HTML5?

The ‘ping’ attribute in HTML5 is used to send a short ‘ping’ request to a specified URL when the user clicks on a link. This can be useful for tracking clicks on specific links without having to use JavaScript. However, it’s important to note that the ‘ping’ attribute is not supported in all browsers.

What is the purpose of the ‘rel’ attribute in HTML5?

The ‘rel’ attribute in HTML5 is used to specify the relationship between the current document and the linked document. This attribute can be used with the ‘a’, ‘link’, and ‘area’ elements. Some common values for the ‘rel’ attribute include ‘stylesheet’ to link to a CSS file, ‘alternate’ to link to an alternate version of the document, and ‘nofollow’ to tell search engines not to follow the link.

How can I use the ‘target’ attribute in HTML5?

The ‘target’ attribute in HTML5 is used to specify where to open the linked document. This attribute can be used with the ‘a’, ‘form’, and ‘base’ elements. Some common values for the ‘target’ attribute include ‘_blank’ to open the link in a new window or tab, ‘_self’ to open the link in the same frame as it was clicked, and ‘_parent’ to open the link in the parent frame.

What is the ‘type’ attribute in HTML5?

The ‘type’ attribute in HTML5 is used to specify the MIME type of the linked resource. This attribute can be used with the ‘a’, ‘link’, ‘object’, ‘script’, and ‘source’ elements. The ‘type’ attribute can be particularly useful when linking to resources that might not be supported by all browsers, as it allows the browser to check if it can support the resource before trying to download it.

How can I use the ‘hreflang’ attribute in HTML5?

The ‘hreflang’ attribute in HTML5 is used to specify the language of the linked resource. This attribute can be particularly useful when linking to versions of your webpage in different languages, as it allows the browser to display the version of the webpage that matches the user’s language settings. The ‘hreflang’ attribute should be used with the ‘a’, ‘link’, and ‘area’ elements.

Ian OxleyIan Oxley
View Author

Ian Oxley has been building stuff on the Web professionally since 2004. He lives and works in Newcastle-upon-Tyne, England, and often attends local user groups and meetups. He's been known to speak at them on occasion too. When he's not in front of a computer Ian can be found playing guitar, and taking photos. But not usually at the same time.

download attributehtml5 downloadhtml5 mediahtml5 pingmedia attribute for linksping attribute
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week