程序目標(biāo):點擊“打開副本網(wǎng)頁”按鈕,然后點擊“發(fā)送信息”向打開的網(wǎng)頁發(fā)送信息,新開的網(wǎng)頁監(jiān)聽并輸出接收的信息。兩個頁面在同個文件夾下網(wǎng)頁1<!DOCTYPE?html>
<html>
<head>
????<meta?charset="utf-8">
????<title>主頁</title>
</head>
<body>
????<button?class="open-copy">打開副本網(wǎng)頁</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)頁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,為什么這個postMessage無法向另一個網(wǎng)頁發(fā)送信息
newsudhc3634140
2016-11-04 21:12:55