Skip to content

Instantly share code, notes, and snippets.

@biswajitpaul01
Created July 28, 2018 14:17
Show Gist options
  • Save biswajitpaul01/3250bb203eafcbafc96e44400f4989de to your computer and use it in GitHub Desktop.
Save biswajitpaul01/3250bb203eafcbafc96e44400f4989de to your computer and use it in GitHub Desktop.
navigator.sendBeacon example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Send Bacon Example</title>
</head>
<body>
<button type="button" id="button">Send Response</button>
<script>
const url = "/print-ajax.php";
button.addEventListener("click", () => {
let formData = new FormData();
formData.append("name", "john does" );
if (navigator.sendBeacon) {
formData.append("from", "sendBeacon");
navigator.sendBeacon(url, formData);
} else {
formData.append("from", "fetch");
fetch(url, {
type: "post",
body: formData
});
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment