如何使用javascript检查打开的窗口是否已关闭?

时间:2011-12-19 07:26:52

标签: javascript

有没有办法检测我打开的哪个弹出窗口是否已关闭?

<script type="text/javascript">
// global variable for subwindow reference
var newWindow;
// generate and fill the new window
function makeNewWindow() {
    // make sure it isn't already opened
        newWindow = window.open("http://www.google.com","sub","status,height=200,width=300");

}

function checkWindow() {
    if(newWindow.closed){
        document.write("Window has closed.");
    }
}
</script>
    <form>
<input type="button" value="Create New Window" onclick="makeNewWindow();" />
<script>
    checkWindow();
</script>
</form>
当我关闭打开的窗口时,我想要那个。函数checkWindow()在屏幕上打印“Window已关闭”。请给我一些帮助代码。提前谢谢

1 个答案:

答案 0 :(得分:2)

  var win = window.open('http://www.google.com','google','width=800,height=600,status=0,toolbar=0');   
  var timer = setInterval(function() {   
  if(win.closed) {  
     clearInterval(timer);  
     alert('closed');  
  }  
}, 1000);