Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iframe cross domain messaging with jQuery postMessage plugin

I am trying to communicate between a child iframe and its parent using the following plugin:

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

I can follow the example and post a message from the child to the parent but not the other way and i really need to be able to communicate both ways.

The code on the parent is as follows:

var origin = document.location.protocol + '//' + document.location.host,
    src = origin + '/Custom/Ui/Baseline/html/iframe-data-cash.htm#' + encodeURIComponent(document.location.href);

$(function () {

    var $holder = $('#iframe'),
        height,
        $iframe = $('<iframe src="' + src + '" id="data-cash-iframe" width="100%" scrolling="no" allowtransparency="true" seamless="seamless" frameborder="0" marginheight="0" marginwidth="0"></iframe>');

    // append iframe to DOM
    $holder.append($iframe);

});

$(window).load(function () {
    $.postMessage(
        'hello world',
        src,
        parent.document.getElementById('data-cash-iframe').contentWindow
    );
});

And the code on the child is as follows:

$(function () {

    var parentURL = decodeURIComponent(document.location.hash.replace(/^#/, ''));

    $.receiveMessage(
        function (e) {
            alert(e.data);
        },
        parentURL
    );

});

I really cannot see why this is not working and am in desperate need of help!

like image 797
RyanP13 Avatar asked Jun 07 '12 11:06

RyanP13


People also ask

Does postMessage work cross domain?

PostMessage() is a global method that safely enables cross-origin communication. It's a lot like Ajax but with cross-domain capability. We'll give it a whirl by setting up two-way communication between a web page and an iframe whose content resides on another server.

How do you communicate between two iframes?

Communicating directly between iframes is also possible by combining window. parent with target as defined above. In conclusion, the postMessage method is a more dynamic alternative to the single DOM, better suited if you load multiple pages in one iframe, but not always easier and it still requires the use of the DOM.

What is Targetorigin postMessage?

postMessage method allows different windows or iframes to communicate directly, even if they were loaded from different origins, circumventing the usual same-origin policy. The sender of the message can restrict the origin of the receiver by specifying a target origin.


2 Answers

1.sendPage
<script type='text/javascript'>
var sendpost = document.getElementById('wrapper').scrollHeight;
var target = parent.postMessage ? parent : (parent.document.postMessage ?   parent.document : undefined); 
target.postMessage(sendpost,'*');
</script>
2. real iframe load page
function handling(e){
                sendIframeHeight = e.data;
}

// <= ie8
if (!window.addEventListener) {
                window.attachEvent("onmessage", handling);
}else{ // > ie8
                window.addEventListener('message', handling, false);
}
like image 65
Korea_hackertop Avatar answered Sep 19 '22 14:09

Korea_hackertop


I had a similar requirement and ended up using postMessage to send data from the child to the parent. Then, to send data from the parent to the child, I passed the data in a query string through the iframe's src attribute. By doing so, I could then parse the query string and retrieve the data within my child page.

like image 24
M Thomas Avatar answered Sep 22 '22 14:09

M Thomas