1. 程式人生 > >js判斷瀏覽器的關閉標籤動作和重新整理的事件

js判斷瀏覽器的關閉標籤動作和重新整理的事件

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		
		<script src="https://unpkg.com/vue"></script>
	</head>
	
	<body>
		<div id="vue01">
			<input placeholder="請輸入" >
			<h1>
				測試關閉瀏覽器
			</h1>			
		</div>
			
		<script>		
			  //關閉瀏覽器提示資訊
			window.onbeforeunload = function (e) {
			  e = e || window.event;
			  var y = e.clientY;
			  if (y <= 0 || y >= Math.max(document.body ? document.body.clientHeight : 0, document.documentElement ? document.documentElement.clientHeight : 0))
			  {
			    //IE 和 Firefox 
			    alert("IE or Firefox");
			    e.returnValue = "確定要重新整理或關閉瀏覽器視窗?";
			  }
			  //谷歌		
			  console.log("beforeclosing");
			  return "確定要重新整理或關閉瀏覽器視窗?";			 
			}
			
		</script>
		
		
		
	</body>
</html>