如果用户正在页面执行比较重要的操作,如写博客,上传文件,此时,如果点其他链接、地址栏输入新地址或刷新页面时,应该给用户提示,确认是否离开当前页面,用JavaScript可轻松完成。

以下是微软官方给出的JS示例,经测试Firefox和Chrome运行正常:

var is_uploading = false;

function alert_if_uploading() {
    if (is_uploading)
        return '文件正在上传中,您确定要离开当前页面?';
}

window.onbeforeunload = alert_if_uploading;

核心代码就是监听window的onbeforeunload事件。

相关文章:

  • 2021-04-04
  • 2022-12-23
  • 2021-05-29
  • 2022-12-23
  • 2021-11-20
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-26
  • 2021-08-01
  • 2022-12-23
  • 2021-10-18
相关资源
相似解决方案