为了从使用
以下是基本代码,用于说明我面临的问题: index.html
postMessage
来传递一些数据的window.open
中返回一个值,我在父窗口(opener)中使用window.addEventListener
,并遇到了一个严重的问题,即回调事件在Internet Explorer 11上永远不会被执行,而在Google Chrome和Microsoft Edge上总是会被执行。以下是基本代码,用于说明我面临的问题: index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<!-- <meta http-equiv="X-UA-Compatible" content="IE=8;IE=9;IE=10;IE=11;IE=edge"> -->
<!-- <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script> -->
</head>
<body>
<p>Click the button to open a new browser window.</p>
<p id="message"></p>
<button onclick="myFunction()">Try it</button>
<script>
var messageEle = document.getElementById('message');
function receiveMessage(e) {
messageEle.innerHTML = "Message Received: " + e.data;
}
window.addEventListener('message', receiveMessage, false);
function myFunction() {
window.open("child.html", "test", "top=500,left=500,width=400,height=400");
}
</script>
</body>
</html>
child.html
<!DOCTYPE html>
<html>
<body>
<p>Child Window</p>
<a href="javascript:;" onclick="sendMessage()">Send Message</a>
<script>
function sendMessage(){
window.opener.postMessage("test", "*");
window.close();
}
</script>
</body>
</html>