简体   繁体   English

跨域iframe通信

[英]Cross-Domain iframe communication

I have an iframe being created on a page, and the page's domain is being explicitly set to 'xyz.com' but the iframe's domain is defaulting to 'dev.xyz.com', which is the actual domain i'm developing for. 我在页面上创建了一个iframe,该页面的域被显式设置为“ xyz.com”,但是iframe的域默认为“ dev.xyz.com”,这是我正在开发的实际域。

The problem is, when I try to access that iframe via iframe.contentWindow.document, it fails due to the difference in domain. 问题是,当我尝试通过iframe.contentWindow.document访问该iframe时,由于域的差异而导致失败。

I've tried setting the iframe's src to a file with document.domain = 'xyz.com' but that doesn't seem to be doing the trick... 我尝试将iframe的src设置为document.domain ='xyz.com'的文件,但这似乎并没有解决问题...

Any ideas? 有任何想法吗?

Page inside iframe: iframe中的页面:

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

It looks silly, but it works. 它看起来很傻,但是可以。 See " What does document.domain = document.domain do? ". 请参阅“ document.domain = document.domain做什么? ”。

After some research, I found this jQuery plugin that makes postMessage backwards-compatible with older browsers using various tricks. 经过一番研究,我发现了这个jQuery插件 ,它使用各种技巧使postMessage与旧版浏览器向后兼容。

Here is a quick example showing how to send the height of the iframe's body to the parent window: 这是一个简单的示例,展示了如何将iframe的高度发送到父窗口:

On the host (parent) page: 在主机(父)页面上:

    // 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.

On the iframe page: 在iframe页面上:

$(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
    );

});

I've tested this on Chrome 13+, Firefox 3.6+, IE7, 8 and 9 on XP and W7, safari on OSX and W7. 我已经在XP和W7上的Chrome 13 +,Firefox 3.6 +,IE7、8和9,在OSX和W7上的野生动物园中对此进行了测试。 ;) ;)

As a addition to the reference to the Ben Alman plug in I thought I would post this working example. 除了引用Ben Alman插件之外,我还想发布这个工作示例。 It ]rRelies on an iframe which has a source page containing jquery authentication & data query script which then passes the result to {other domain} parent window using the message plugin. 它依赖于一个iframe,该iframe的源页面包含jquery身份验证和数据查询脚本,然后使用消息插件将结果传递到{other domain}父窗口。

NB message plugin will break if using JQ v9 as JQV9 does not use "browser" referenced in the plugin 如果使用JQ v9,则NB消息插件将中断,因为JQV9不使用插件中引用的“浏览器”

1st step: Add the plugin code to both sending and receiving docs: 第一步:将插件代码添加到发送和接收文档中:

http://benalman.com/projects/jquery-postmessage-plugin/ http://benalman.com/projects/jquery-postmessage-plugin/

2nd step: Add this to sending doc: 第二步:将其添加到发送文档中:

   $.postMessage(
$(X).html(),    
'http://DOMAIN [PORT]/FOLDER/SUBFOLDER/RECIEVINGDOCNAME.XXX'   
 )  ;      

Where X can be a local var containing preformatted json array or other stuff, and the http url here is the address of the receiving document. 其中X可以是包含预格式化json数组或其他内容的本地var,此处的http url是接收文档的地址。

3rd step: Add this to receiving doc: 第三步:将其添加到接收文档中:

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

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

);

Where the http url is the DOMAIN of the sending document. 其中http url是发送文档的DOMAIN。 Good in IE 8, 9, FF16, safari Windows (windows wait x V9 not tested yet), safari x mac thing. 擅长IE 8、9,FF16,safari Windows(Windows等待x V9尚未测试),safari x mac之类的东西。

The result is any item you want out of another domain page (that you have access to..). 结果是您想要从另一个域页面中退出的任何项目(您有权访问..)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM