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!
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.
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.
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.
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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With