2014-10-12 1 views
3

여러 창을 여는 웹 응용 프로그램이 있습니다. 내가 가지고있는 문제는 부모 창이 닫히거나 새로 고쳐지면 하위 창이 열린 채로 남아 있다는 것입니다. onunloadonbeforeunload을 사용해 보았지만 아무 것도 Chrome 및 Firefox의 창 닫기 이벤트를 포착하지 않았습니다. 창이 배열되어 있지만 새로 고친 후 참조가 손실됩니다.부모 창을 닫을 때 모든 하위 창 닫기

이 이벤트를 수신하는 다른 방법이 있습니까? (페이지를 새로 고칠 때 closeAll() 외부 unloadonbeforeunload 내 모든 열려있는 창을 닫고 실행 있지만)

내 창을 닫는 관련 코드 :

window.unload = function() { 
    closeAll(); 
} 
window.onbeforeunload = function() { 
    closeAll(); 
} 

var closePopup = function(popup) { 
    removePopup(popup); 
    popup.close(); 
}; 

var closeAll = function() { 
    for (var popup in _this.popups) { 
     closePopup(_this.popups[popup]); 
    } 
} 

이는 크롬에서가 아니라 파이어 폭스에서 작동 및 IE (최신 버전).

+1

는 * "나는으로 onUnload 및 onbeforeunload를 사용하여 시도했지만 그들 중 누구도 (크롬과 파이어 폭스에서) 창 닫기 이벤트를 잡는다 없다"*'절대적으로 파이어 폭스와 크롬에서 작업을 수행 onbeforeunload'. –

+0

"* 아무도 윈도우 닫기 이벤트 *를 포착하지 못했습니다."라는 사실은 T.J. Crowder가 지적한 바에 따르면, 이들은 지정된 브라우저에서 작동합니다. 당신의 시도를 보여 주시겠습니까? 그러면 우리가 도울 수있을 것입니다. –

+0

이 코드는 Chrome에서는 작동하지만 Firefox 및 IE에서는 작동하지 않는 것으로 보입니다. 이 이벤트 밖에서'closeAll' 함수를 테스트 해 보았습니다. 또한 여기에 여러 참조를 발견했기 때문에 Firefox 및 다른 브라우저에서는 작동하지 않습니다. http://stackoverflow.com/questions/14645011/window-onbeforeunload-and-window-onunload-is-not-working-in -firefox-safari-o http://stackoverflow.com/questions/20773306/mozilla-firefox-not-working-with-window-onbeforeunload – flaviu

답변

1

적절한 솔루션 크롬, 파이어 폭스와 IE의 최신 버전에서 작동 (jQuery를).

처음에는 jQuery $().unload() 함수 (http://api.jquery.com/unload/)를 사용하려고했지만 1.8 (http://bugs.jquery.com/ticket/11733) 이후로 사용되지 않습니다. $().unload()$().bind('unload', fn)의 바로 가기이기 때문에 나는 기본 하나를 사용해 보았고 효과가있었습니다. 당신이 window.open 모든 자식 창을 열면

$(window).on('unload', function() { 
    closeAll(); 
}); 
+0

부모 창만 닫으면 하위 창은 닫히지 않습니다. – clarifier

+1

@clarifier 그 이유는 closeAll() 함수가 모든 팝업 창 객체 인스턴스를 통해 루프를 실행하고 close()를 호출해야하기 때문일 수 있습니다. – Sawtaytoes

2

사용을 찾을 수이

var popup = window.open("popup.html", "popup", "width=200,height=200"); 

window.onunload = function() { 
    if (popup && !popup.closed) { 
     popup.close(); 
    } 
}; 
+0

저는 윈도우 배열을 가지고 closeAll() 함수를 호출합니다. 내 코드는 솔루션과 비슷하지만 크롬에서만 작동한다고 생각합니다. Chrome 이외의 브라우저에 대한이 응답을 찾았습니다. http://stackoverflow.com/questions/14645011/window-onbeforeunload-and-window-onunload-is-not-working-in-firefox-safari-o 알 수 없습니다. 귀하의 솔루션은 크로스 브라우저입니다. – flaviu

1

,이 모든 페이지에 자바 스크립트, 다음 CLOSEALL (거짓)를 포함한다; 모든 포함 된 페이지의은 모든 자식, 손자, 위대한 ... 등을 닫고 첫 번째 (루트) 페이지를 login.aspx로 리디렉션하며 intial 처리기를 통과 시키므로 이벤트 트리거를 방해하지 않습니다.

function CloseAll(bTopFound) 
{ 
    if (!bTopFound && nParent != null && !nParent.closed) { 
     //parent window was not closed 
     nParent.CloseAll(false); 
     return; 
    } else { 
     if (nParent == null || nParent.closed) 
     { 
      top.location = '/login.aspx'; 
     } 
    } 

    for (var i = 0; i < Windows.length; i++) 
    { 
     if (!Windows[i].closed) { 
      Windows[i].CloseAll(true); 
     } 
    } 
    nParent = null; 
    setTimeout(window.close, 150); 
} 

var Windows = []; 
//override window.open to inject store child window objects 
window.open = function (open) { 
    return function (url, name, features) { 
     // set name if missing here 
     name = name || "default_window_name"; 
     var newWindow = open.call(window, url, name, features); 
     Windows.push(newWindow); 
     return newWindow; 
    }; 
}(window.open); 

var nParent = null; 
window.onload = function (load) { 
    return function (e) { 
     nParent = window.opener; 
     if (load != null) { 
      load.call(e); 
     } 
    } 
}(window.onload); 

window.onunload = function (unload) { 
    return function (e) { 
     //promote first branch page to parent 
     if (nParent != null && !nParent.closed && Windows.length > 0) { 
      nParent.Windows.push(Windows[0]); 
      Windows[0].nParent = nParent; 
     } 
     //make first child window new root 
     for (var i = 1; i < Windows.length; i++) { 
      Windows[i].nParent = Windows[0]; 
      Windows[0].Windows.push(Windows[i]); 
     } 
     if (unload != null) { 
      unload.call(e); 
     } 
    } 
}(window.onunload); 
관련 문제