星期四, 7月 05, 2012

偵測新視窗的close event

如何偵測新視窗的close event咧
<script type="text/javascript">
    var win = window.open("secondwindow.html", "_blank");
    win.onunload =onun; 

    function onun() {
        if(win.location != "about:blank") // This is so that the function 
                                          // doesn't do anything when the 
                                          // window is first opened.
        {
            alert("closed");
        }
    }
</script>
不過這方法不能跨網域
也就是如果child開到不同網域,close不會觸發unload事件
解決方法先轉個空白的網頁,裡面夾iframe去連該網頁

後記-跨網頁問題
被同事嗆「閃開! 讓專業的來」 見下面程式碼
也不去bind event,只是持續monitor關了沒...真是好招...
var child = window.open('http://google.com','','toolbar=0,status=0,width=626,height=436');
var timer = setInterval(checkChild, 500);

function checkChild() {
    if (child.closed) {
        alert("Child window closed");   
        clearInterval(timer);
    }
}


References

沒有留言: