如何關閉所有window.open的網頁

  • 555
  • 0

透過JS關閉所有之前透過window.open開啟的網頁,通常會用在登出的時候,

系統登出之後會將開啟的分頁通通關閉。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-1.6.4.js"></script>
<input type=button id = "1" value="Google">
  <input type=button id = "2" value="yahoo">
  <input type=button id = "3" value="Bing">
  <input type=button id = "4" value="CloseAll">
  
</body>
</html>

現在有四個按鈕,前三個按鈕可以開全新的分頁,

最後一個按鈕點選的時候可以關閉所有剛剛開過的分頁

JS:


var windows = [];

$("#1").click(function(){
                   
  var winGoogle = window.open('http://google.com', '_blank');
  windows.push(winGoogle);
});
$("#2").click(function(){
                   
   var winBing =window.open('http://bing.com', '_blank');
  windows.push(winBing);
});
$("#3").click(function(){
                   
  var winYahoo = window.open('http://yahoo.com', '_blank');; 
  windows.push(winYahoo);
});
$("#4").click(function(){
 for(var i = 0; i < windows.length; i++){
    windows[i].close()
}
});

以上,謝謝小風XD