兩個重要的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;”> </a>
轉自 : https://www.jianshu.com/p/583ccf352dcd
經測試
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>
轉自 http://blog.sina.com.cn/s/blog_9e42ae520102ynly.html
獲取不到
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>