window.open 浏览器托管下载并监听下载完成

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
	我是首页
		<script>
			var url  = 'http://182.48.114.81:9406/dns/engine/domain/c2/csv/export?token=63b4831f403e68148791d1eb89f7c62e';
			setTimeout(function(){
				var net = window.open(url,'_self','',false);
				 net.addEventListener("beforeunload", (e) => {
					document.write("下载完成")
				 });
			},3000)
		</script>
	</body>
</html>

有个缺陷 就是放在react代码中会无法正确运行,这是因为这里的监听完成其实是通过 窗口之间的切换完成的,在react中,切换窗口会卸载组件。导致监听事件的回调函数中无法运行。

原文地址:https://www.cnblogs.com/chengyunshen/p/12411718.html