两个重要的javascript语句:

top.result.document.location='result.html?data=' + data; --------url传参长度有限制。。。。

window.top.frames['result'].document.getElementById("an").innerHTML=data;

都很好理解, 第一个就是js重新载入父窗口中name为result的iframe页面, 第二个就是直接通过选择另一个iframe中的元素来赋值.

 

转自 :http://www.imooc.com/article/252909?block_id=tuijian_wz

 

 




//将需要用到的参数 拼接成json串

jsontab = ‘{“url”:”‘ + url + ‘”,”dataId”:”‘ + dataId + ‘”,”text”:”‘ + text + ‘”,”m”:”‘ + m + ‘”}’;

addp();

})

function addp(){

//将json串赋给父级窗体元素

window.parent.document.getElementById(‘hidInp’).value=jsontab;

//addTabpp是父级窗体中 写有onclick方法的元素

window.parent.document.getElementById(‘addTabpp’).click();

};

 

//父级窗体

//隐藏域及触发方法的a标签

<input type=”text” id=”hidInp” value=”hidtext” style=”opacity:0;”>

<a href=”javascript:void(0);” id=”addTabpp” onclick=”addpp()”” style=”opacity:0;”>           &nbsp;</a>

 

经测试 



var sb1 = new HandleHelper.MyStringBuilder();
sb1.appendFormat('<iframe id="if1" src="http://localhost:52768/web/Dict_Edit.html" style="width: 100%;"></iframe>');
document.getElementById("iframe1").innerHTML = sb1.toString();
var if1 = document.getElementById("if1");

if1.contentWindow.postMessage("Hello", "http://localhost:52768/web/Dict_Edit.html"); //发送消息,直接写写在dom中是可以的,. 动态dom 不起作用,
    或者是先写入iframe, 在发送数据, 在设置src;



var sb1 = new HandleHelper.MyStringBuilder();
sb1.appendFormat('<iframe id="if1" style="width: 100%;"></iframe>');
document.getElementById("iframe1").innerHTML = sb1.toString();
var if1 = document.getElementById("if1");
if1.src = "http://localhost:52768/web/Dict_Edit.html";
if1.contentWindow.postMessage("Hello", "http://localhost:52768/web/Dict_Edit.html"); //发送消息 测试 不起作用...



<script>
window.onload = function () {
var messageEle = document.getElementById('message');
window.addEventListener('message', function (e) {
alert(e.origin);
if (e.origin !== "http://www.42du.cn") {
return;
}
messageEle.innerHTML = "从" + e.origin + "收到消息: " + e.data;
});
}
</script>


 

获取不到

var if1 = document.getElementById("if1");

if1.src = "http://localhost:52768/web/Dict_Edit.html";

//if1.contentWindow.postMessage("Hello", "http://localhost:52768/web/Dict_Edit.html"); //发送消息 在加载之前???

var t = window.frames;

var a = if1.contentDocument.getElementsByTagName("body");

var an = a[0];

var cc = an.getElementsByClassName("an");

 

 

------最终   子窗体 获取父窗体

<script>

window.onload = function () {

var t = window.parent.document.getElementById("AAA").innerHTML;

alert(t);

}

</script>