2010-07-03 3 views
4

iframe 내부에서 unbeforeunload 이벤트를 설정하면 iframe의 링크를 클릭 할 때 트리거되지 않습니다. 그것은 IE와 Firefox에서 작동하지만 Opera는 일반적인 AFAIK에서 onbeforeunload를 지원하지 않습니다.iframe의 onbeforeunload 이벤트가 Google 크롬에서 실행되지 않습니다. Firefox에서 작동합니다.

내가 궁금한 점이 있습니까? 이 행동이 가능하지 않아야합니까? 아니면 Google 크롬/웹킷의 버그입니까? 해결 방법은 무엇입니까?

코드 예제 :

인 test.html :

<html> 
    <body> 
    <p>Main page content</p> 
    <script type="text/javascript"> 
     window.onbeforeunload = function() {alert('unloadevent in main window');}; 
    </script> 
    <iframe id="tpa" src="..test2.html"></iframe> 
    </body> 
</html> 

test2.html :

<html> 
    <body> 
    <script type="text/javascript"> 
     self.onbeforeunload = function() {alert('unloadevent in frame window');}; 
    </script> 
    <a href="http://www.google.com/">Link to for instance google</a> 
    </body> 
</html> 

답변

1

대체 방법으로 문서 수준에서 iframe 링크의 click 이벤트를 처리하거나 iframe 페이지를 사전 처리하고 사용자 지정 JS 처리기를 실행하는 링크를 변경할 수 있습니다. 그런 다음 비즈니스 규칙에 따라 클릭 이벤트 전파를 억제하십시오.

2

나는 그것을 고치기위한 완고한 방법을 발견했습니다. 내 "c.d.com"에서

<html> 
<iframe id="OBU" name="OBU" src="c.d.com"> 
</iframe> 
<script> 
    window.onbeforeunload = function() { 
     window.frames["OBUI"].frames["SubOBUI"].location = 
       "a.b.com/demo.jpg?t="+(new Date()).getTime(); 
    } 
</script> 
</html> 

:

<iframe id="SubOBUI" name="SubOBUI" src="a.b.com/demo.jpg" 
     onload="someMethodThatShouldBeExecutedWhileUnload"></iframe> 
<script> 
    var someMethodThatShouldBeExecutedWhileUnload : function() { 
      alert(1); 
    } 
</script> 

가 보라고 도메인 "a.b.com"을 보유하고 내 parentPage에서 !

관련 문제