Событие onafterprint работает только один раз

Мне нужно выполнить собственную процедуру после вызова функции печати браузера. Проблема в том, что она выполняется только один раз.

Что я могу сделать, чтобы событие onafterprint всегда выполнялось?

window.onafterprint = function() {
  location.href = '{% url 'logout_' %}';
}

Swal.fire({
  title: 'Example',
  showDenyButton: true,
  showCancelButton: true,
  confirmButtonText: 'Yes',
  confirmButtonColor: 'green',
  denyButtonText: 'No',
  cancelButtonText: 'Cancel'
}).then((result) => {
  if (result.isConfirmed) {
    flPrint = true;
  } else if (result.isDenied) {
    flPrint = false;
  }

  if ((result.isConfirmed) || (result.isDenied)) {
    $.ajax({
      url: '{% url 'ajax-view' %}',
      dataType: 'json',
      contentType: "application/json",
      type: 'GET',
      success: function(r) {
        if (r.result) {
          if (flPrint) {
            window.print();
          } else {
            location.href = '{% url 'logout_' %}';
          }
        }
      },
      error: function(err) {
        console.log(err);
      }
    });
  }
})
Вернуться на верх