Cross-Domain iframe communication

前端 未结 3 862
我寻月下人不归
我寻月下人不归 2020-12-08 05:45

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 t

3条回答
  •  长情又很酷
    2020-12-08 06:05

    As a addition to the reference to the Ben Alman plug in I thought I would post this working example. 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.

    NB message plugin will break if using JQ v9 as JQV9 does not use "browser" referenced in the plugin

    1st step: Add the plugin code to both sending and receiving docs:

    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.

    3rd step: Add this to receiving doc:

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

    '+event.data+'

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

    Where the http url is the DOMAIN of the sending document. Good in IE 8, 9, FF16, safari Windows (windows wait x V9 not tested yet), safari x mac thing.

    The result is any item you want out of another domain page (that you have access to..).

提交回复
热议问题