Skip to content
This repository has been archived by the owner on Aug 4, 2024. It is now read-only.

Commit

Permalink
If navigator.sendBeacon can not queue the work for transferring, retu…
Browse files Browse the repository at this point in the history
…rn false as per the specification.

Fixes https://github.com/Financial-Times/polyfill-service/issues/2160
  • Loading branch information
JakeChampion committed Mar 3, 2020
1 parent b2d0ac9 commit 452d18b
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions polyfills/navigator/sendBeacon/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
/* global ActiveXObject */
if (!('navigator' in self)) self.navigator = {};
self.navigator.sendBeacon = function sendBeacon(url, data) {
var xhr = ('XMLHttpRequest' in window) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xhr.open('POST', url, false);
xhr.setRequestHeader('Accept', '*/*');
if (typeof data === 'string') {
xhr.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8');
} else if (Object.prototype.toString.call(data) === '[object Blob]') {
if (data.type) {
xhr.setRequestHeader('Content-Type', data.type);
try {
var xhr = new XMLHttpRequest();
xhr.open('POST', url, false);
xhr.onerror = function() {};
xhr.setRequestHeader('Accept', '*/*');
if (typeof data === 'string') {
xhr.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8');
} else if (Object.prototype.toString.call(data) === '[object Blob]') {
if (data.type) {
xhr.setRequestHeader('Content-Type', data.type);
}
}
xhr.send(data);
return true;
} catch (error) {
return false;
}
xhr.send(data);
return true;
};

0 comments on commit 452d18b

Please sign in to comment.