禁用OnBeforeUnload事件:如何将Javascript发送到跨域Iframe?

时间:2010-12-06 20:39:37

标签: jquery load cross-domain onbeforeunload

我想要完成一些非常简单的事情。 我指的是通过iframe的网站。

iframe有一个调用“onBeforeUnload”的函数。 我希望在iframe中从我的父页面卸载前禁用。

我已经想出了如何在SAME域和同一页面上执行此操作。但是,我如何为跨域域执行此操作?

我会做任何事情,无论多么复杂,都可以做到这一点。

<iframe src="http://www.domain.com" width="100%" height="500" id="iframe"></iframe> 

<script type="text/javascript">

 $(document).ready(function () {

 $('#iframe').load( function() {
      alert('Load was performed.'); 
    document.getElementById('iframe').contentWindow.window.onbeforeunload=null; 
//NOTE: DOESN'T WORK CROSS DOMAIN! 
    });

    window.onbeforeunload=null  //Disables onbeforeunload event on THIS page so long as it is AFTER the original onbeforeunload event

    });

</script>

谢谢!

2 个答案:

答案 0 :(得分:0)

由于同源策略,这是不可能的,除非您实际上也在iframe中运行了自己的代码。

在这种情况下,您可以使用easyXDM来执行此操作 - 但如果是这种情况,您只需直接删除处理程序吗?

答案 1 :(得分:0)

在主页的onbeforeunload事件中使包含iframe的div的innerhtml等于空字符串将停止警报。 使用onbeforeunload主页,因为离开页面时不使用iframe。