상황별 정리
상황 | 자식창에서 부모창 제어방법 |
iframe 안에서 | window.parent.location.href = '/member/login'; |
새 창 (window.open) | window.opener.location.href = '/member/login'; |
사용 예제 : 새 창에서 부모 로그인 페이지로 보내기
if (window.opener) {
// 새 창을 띄운 부모창이 존재할 때
window.opener.location.href = '/member/login';
window.close(); // 필요하면 자식창도 닫기
} else {
// 부모창이 없으면 자기 자신 이동
window.location.href = '/member/login';
}
서버단 인터셉터에서 응답 내보낼땐 이렇게
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<script>");
out.println("alert('로그인 세션이 만료되었습니다. 다시 로그인 해주세요.');");
out.println("if (window.opener) {");
out.println(" window.opener.location.href='/member/login';");
out.println(" window.close();");
out.println("} else {");
out.println(" window.location.href='/member/login';");
out.println("}");
out.println("</script>");
out.flush();
- window.opener는 새 창(window.open)을 띄운 부모 창 객체
- 보안상 window.opener 접근이 안 되는 경우도 있어서 같은 도메인 내에서만 가능하다.
- iframe이랑은 전혀 별개의 객체니까 혼동 주의!
'JS, React' 카테고리의 다른 글
# jQuery - 동적으로 생성된 요소에 click 이벤트가 안 먹힐 때 해결법 (0) | 2025.06.09 |
---|---|
# iframe 부모에 focus 옮기기 (모달창 ESC 안닫힘) (0) | 2025.04.01 |
# fetch 비동기 통신 (0) | 2025.03.21 |