我尝试使用以下插件孩子iframe和母公司之间的沟通:iframe的跨域通讯与jQuery的postMessage插件
http://benalman.com/projects/jquery-postmessage-plugin/
我可以效仿的榜样,并从孩子到发布消息父母但不是其他方式,我真的需要能够沟通两种方式。
父的代码如下:
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
);
});
而且对孩子的代码如下:
$(function() {
var parentURL = decodeURIComponent(document.location.hash.replace(/^#/, ''));
$.receiveMessage(
function (e) {
alert(e.data);
},
parentURL
);
});
我实在看不出这是为什么不工作,我在绝望需要帮助!