程序目標(biāo):點(diǎn)擊“打開(kāi)副本網(wǎng)頁(yè)”按鈕,然后點(diǎn)擊“發(fā)送信息”向打開(kāi)的網(wǎng)頁(yè)發(fā)送信息,新開(kāi)的網(wǎng)頁(yè)監(jiān)聽(tīng)并輸出接收的信息。兩個(gè)頁(yè)面在同個(gè)文件夾下網(wǎng)頁(yè)1<!DOCTYPE?html>
<html>
<head>
????<meta?charset="utf-8">
????<title>主頁(yè)</title>
</head>
<body>
????<button?class="open-copy">打開(kāi)副本網(wǎng)頁(yè)</button>
????<button?class="send-msg">發(fā)送信息</button>
????<script?type="text/javascript">
????????var?openCopy?=?document.getElementsByClassName("open-copy")[0];
????????var?sendMsg?=?document.getElementsByClassName("send-msg")[0];
????????openCopy.onclick?=?function(){
????????????newPage?=?window.open("practice2.html");
????????};
????????sendMsg.onclick?=?function(){
????????????newPage.postMessage("nihao","practice2.html");
????????}
????</script>
</body>
</html>網(wǎng)頁(yè)2<!DOCTYPE?html>
<html>
<head>
????<meta?charset="utf-8">
????<title>副本</title>
</head>
<body>
????<button?class="get-msg">接收打印信息</button>
????<script?type="text/javascript">
????????var?getMsg?=?document.getElementsByClassName("get-msg")[0];
????????window.addEventListener("message",function(e){
????????????newMsg?=?document.write(e.data);
????????????getMsg.onclick?=?function(){
????????????????console.log(e.data);
????????????}
????????},false);
????</script>
</body>
</html>
js,為什么這個(gè)postMessage無(wú)法向另一個(gè)網(wǎng)頁(yè)發(fā)送信息
newsudhc3634140
2016-11-04 21:12:55