重新加载后显示吐司通知

时间:2018-04-05 01:56:40

标签: javascript ajax

我想在重新加载页面后显示一个Toast通知,表示文件已上传。这是我到目前为止所得到的

_fileUploads.delete = function(reload_on_return) {
  var filtered = root.fileUploads().filter(_ => _._id() == _fileUploads._id());
  var index = root.fileUploads.indexOf(filtered = filtered[0]);
  filtered = ko.toJS(filtered);

  swal({
    text: 'Are you sure you want to delete this file?',
    buttons: true,
    dangerMode: true,
    icon: 'warning'
  }).then(function (allowDelete) {
    if (allowDelete) {
      $.ajax({
        type: 'DELETE',
        url: '/api/gridfs/files/' + filtered._id,
        statusCode: {
          204: function(response) {
            toastrTrigger('The File has been Deleted')
            if (reload_on_return) {
              setTimeout( function() {
                location.reload();
              }, 0001);    
            }
          }
        },
        error: function (xhr, status, error) {
          console.log(xhr);
        }
      });
    }
  });
}

这只刷新页面而不显示通知

这是toastrtrigger函数()



function toastrTrigger(message, title, type) {
  setTimeout(function() {
    toastr.options = {
      closeButton: true,
      progressBar: true,
      showMethod: 'slideDown',
      timeOut: 4000
    };
    toastr[type || "success"](message, title || 'File Uploads Repository');
  }, 500);
}




1 个答案:

答案 0 :(得分:1)

重新加载页面后,脚本不会持久存储:文档关闭后,与文档关联的所有脚本都会随之消失。没有办法解决这个问题。您必须以某种方式将信息传递到您导航到纯粹通过URL导航的页面。

解决方案是将查询字符串传递给重新加载的页面:

if (reload_on_return) {
  window.location.href = window.location.pathname + '?deleteSuccess=1';
}

然后,在同一页面上,在页面加载时,检查是否存在查询字符串:

const { search } = window.location;
const deleteSuccess = (new URLSearchParams(search)).get('deleteSuccess');
if (deleteSuccess === '1') {
  // The page was just reloaded, display the toast:
  toastrTrigger('The file has been deleted');
}