---
title: "The ping attribute sends a POST when a link is clicked · Ihab Khattab"
description: "The ping attribute on <a> makes the browser send an asynchronous POST request to a URL when the link is clicked. You might need this for tracking clicks…"
url: "https://ihabkhattab.com/til/anchor-ping-attribute/"
---

# The ping attribute sends a POST when a link is clicked

Jul 16, 2020 · [Also on X](https://x.com/HO_BA/status/1283538922098827265)

The `ping` attribute on `<a>` makes the browser send an asynchronous `POST` request to a URL when the link is clicked. You might need this for tracking clicks without redirecting through your own server first. Google uses it in search results, with a workaround for Firefox, which disables `ping` by default.

```html
<!-- Sends an asynchronous POST request to /track -->
<a href="https://www.reddit.com/" ping="/track">reddit</a>

<!-- Ping several URLs, separated by spaces -->
<a href="https://www.reddit.com/" ping="/track https://tracking.example/">reddit</a>
```

The request body is just `PING` with `Content-Type: text/ping`, and the `Ping-From` and `Ping-To` headers tell the server which page the click came from and where it went.

For tracking things other than link clicks, the [Beacon API](https://developer.mozilla.org/en-US/docs/Web/API/Beacon_API) (`navigator.sendBeacon()`) sends a request that survives the page unloading. It was still experimental when I tweeted this; it now works in every major browser.

More: [Spec: hyperlink auditing](https://html.spec.whatwg.org/multipage/links.html#hyperlink-auditing) · [Video](https://www.youtube.com/watch?v=3AIswRtblYY)

[← All TILs](https://ihabkhattab.com/til/)
