Javascript 跨域iframe通信

Javascript 跨域iframe通信,javascript,iframe,cross-domain,Javascript,Iframe,Cross Domain,我在页面上创建了一个iframe,页面的域被显式设置为“xyz.com”,但iframe的域默认为“dev.xyz.com”,这是我正在开发的实际域 问题是,当我试图通过iframe.contentWindow.document访问该iframe时,由于域的不同,它失败了 我已经尝试过将iframe的src设置为document.domain='xyz.com'的文件,但这似乎不起作用 有什么想法吗?iframe内的页面: <script> document.domain = doc

我在页面上创建了一个iframe,页面的域被显式设置为“xyz.com”,但iframe的域默认为“dev.xyz.com”,这是我正在开发的实际域

问题是,当我试图通过iframe.contentWindow.document访问该iframe时,由于域的不同,它失败了

我已经尝试过将iframe的src设置为document.domain='xyz.com'的文件,但这似乎不起作用

有什么想法吗?

iframe内的页面:

<script>
document.domain = document.domain;
</script>

document.domain=document.domain;

这看起来很傻,但很管用。参见“”

经过一些研究,我发现这使得postMessage向后兼容使用各种技巧的旧浏览器

下面是一个快速示例,演示如何将iframe主体的高度发送到父窗口:

在主机(父级)页面上:

    // executes when a message is received from the iframe, to adjust 
    // the iframe's height
    $.receiveMessage(
        function( event ){
            $( 'my_iframe' ).css({
                height: event.data
            });
    });
   // Please note this function could also verify event.origin and other security-related checks.
$(function(){

    // Sends a message to the parent window to tell it the height of the 
    // iframe's body        
    var target = parent.postMessage ? parent : (parent.document.postMessage ? parent.document : undefined);

    $.postMessage(
        $('body').outerHeight( true ) + 'px',
        '*',
        target
    );

});
在iframe页面上:

    // executes when a message is received from the iframe, to adjust 
    // the iframe's height
    $.receiveMessage(
        function( event ){
            $( 'my_iframe' ).css({
                height: event.data
            });
    });
   // Please note this function could also verify event.origin and other security-related checks.
$(function(){

    // Sends a message to the parent window to tell it the height of the 
    // iframe's body        
    var target = parent.postMessage ? parent : (parent.document.postMessage ? parent.document : undefined);

    $.postMessage(
        $('body').outerHeight( true ) + 'px',
        '*',
        target
    );

});

我已经在Chrome13+、Firefox3.6+、XP和W7上的IE7、8和9、OSX和W7上的safari上对此进行了测试

作为对Ben Alman的补充 插件我想我会发布这个工作示例。它位于iframe上,iframe有一个包含jquery身份验证和数据查询脚本的源页面,然后使用消息插件将结果传递到{other domain}父窗口

注意:如果使用JQV9,消息插件将中断,因为JQV9没有使用插件中引用的“浏览器”

第一步: 将插件代码添加到发送和接收文档:

第二步: 将此添加到发送单据:

   $.postMessage(
$(X).html(),    
'http://DOMAIN [PORT]/FOLDER/SUBFOLDER/RECIEVINGDOCNAME.XXX'   
 )  ;      
其中X可以是包含预格式化json数组或其他内容的本地变量,这里的http url是接收文档的地址

第三步: 将其添加到接收单:

    $.receiveMessage(
    function(event){
        alert("event.data: "+event.data);
                $("#testresults").append('<h1>'+event.data+'<h1>');

    },          
    'http://DOMAIN.COM OR SOMETHING'

);
$.receiveMessage(
功能(事件){
警报(“event.data:+event.data”);
$(“#测试结果”).append(“”+event.data+“”);
},          
'http://DOMAIN.COM 或者什么
);
其中http url是发送文档的域。 在IE 8、9、FF16、safari Windows(Windows等待x V9尚未测试)、safari x mac方面表现出色


结果是您希望从另一个域页面(您有权访问..)中删除的任何项目。

我认为您需要将iframe的document.domain和主页的document.domain设置为相同的内容。()作为后续,下面是创建iframe并尝试访问它的代码:jQuery(“body”).prepend(“”);var ihistory=jQuery(“#jQuery_历史”)[0];var iframe=ihistory.contentWindow.document;由于您可以访问父对象和iframe,我建议您阅读以下内容:Ben Alman有一个很棒的jquery插件,可以用来解决这个问题。这个库支持HTML5 postMessage和带有resize+hash的旧式浏览器(Ben Alman的jQuery插件已经3年没有被使用过了)。当您需要访问控制允许原始请求时,这个肮脏的黑客会干扰您,就像JS加载的AJAX中的CDN一样。使用时要小心。