检查新窗口是否被关闭

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.  
  5. <script>
  6. var myWindow;
  7. function openWin() {
  8. myWindow = window.open("", "", "width=400 ,height=200");
  9. }
  10.  
  11. function closeWin() {
  12. if (myWindow) {
  13. myWindow.close();
  14. }
  15. }
  16.  
  17. function checkWin() {
  18. msg = ""
  19. if (!myWindow) {
  20. msg = "从未打开";
  21. } else {
  22. if (myWindow.closed) {
  23. msg = "已关闭";
  24. } else {
  25. msg = "已打开";
  26. }
  27.  
  28. }
  29. document.getElementById("msg").innerHTML =
  30. "myWindow " + msg;
  31. }
  32. </script>
  33.  
  34. </head>
  35. <body>
  36.  
  37. <button onclick="openWin()">打开 myWindow</button>
  38. <button onclick="closeWin()">关闭 myWindow</button>
  39. <button onclick="checkWin()">myWindow 打开了吗?</button>
  40.  
  41. <br><br>
  42. <div id="msg"></div>
  43.  
  44. </body>
  45. </html>