通过 1*1 像素的空白 gif 进行埋点数据上报

  1. image 天然支持跨域
  2. 不会阻塞页面,用户体验好
  3. 图片类型中,体积最小
  4. 不会占用 AJAX 的请求限额

demo

  1. const img = new Image();
  2. img.onload = () => {
  3. console.log('success');
  4. };
  5. img.onerror = () => {
  6. console.log('error');
  7. };
  8. img.src = 'http://www.baidu.com?a=1&b=2';
  9. // 封装
  10. const track = ({ success = () => {}, error = () => {}, url }) => {
  11. const img = new Image();
  12. img.onload = () => success();
  13. img.onerror = () => error();
  14. img.src = url;
  15. };