var is_uploading = false; function alert_if_uploading() { if (is_uploading) return '文件正在上传中,您确定要离开当前页面?'; } window.onbeforeunload = alert_if_uploading;
核心代码就是监听window的onbeforeunload事件。