2011-12-30 5 views
4

iframe의 언로드 이벤트를 제거 할 때 어떻게 트리거 될 수 있습니까?iframe을 제거 할 때 onunload 이벤트를 트리거하는 방법은 무엇입니까?

아래 코드를 사용하면 removeChild (iframe)가 호출 될 때 onunload 이벤트가 트리거되지 않습니다.

<!-- parent.html --> 
<html> 
    <head> 
    <title>remove iframe</title> 
    <script type="text/javascript"> 
     window.onload = function() { 
     var button = document.getElementsByTagName("BUTTON")[0]; 
     button.onclick = function() { 
      document.body.removeChild(document.getElementsByTagName("IFRAME")[0]); 
     }; 
     }; 
    </script> 
    </head> 
    <body> 
    <iframe src="./son.html" /> 
    <button>Remove iframe</botton> 
    </body> 
</html> 
<!-- son.html --> 
<html> 
    <head> 
    <title>son html</title> 
    <script type="text/javascript"> 
     window.onload = function() { alert("hello"); }; 
     window.onunload = function() { alert("world!"); }; 
    </script> 
    </head> 
    <body style="background-color:#aaccee;"> 
    </body> 
</html> 

어떻게 트리거 할 수 있습니까?

답변

8

프레임을 제거하기 전에 iframe 요소의 src 특성을 "about : blank"로 설정할 수 있습니다. iframe 트리거 onunload 이벤트를 만들지 확인하십시오.

EG :

var iframe = document.getElementsByTagName("IFRAME")[0]; 
iframe.src = "about:blank"; 
document.body.removeChild(iframe); 
+0

은 크롬에서 파이어 폭스에서 잘 작동하지만,하지. 어떤 아이디어? – ipinyol