我是堆栈溢出的新手——很高兴加入像你这样的社区。
我正在发送此消息,因为我正在尝试进行有关跨窗口通信的活动,但我无法弄清楚我做错了什么。该任务仅包括从源发送消息,并将其显示在目标窗口上。我使用的网络浏览器是 Firefox 94.0.1(64 位)。
这里有我写的代码:
起源
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Origin</title>
</head>
<body>
<script>
function sendMessage() {
var destination = window.open("http://localhost/dew/cross_window_communication/destination.html");
destination.postMessage("Message from localhost", "http://localhost/dew/cross_window_communication/origin.html");
}
</script>
<form>
<input type="button" onclick="sendMessage();" value="Send Message">
</form>
</body>
</html>
目的地
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Destination</title>
</head>
<body>
<script>
window.addEventListener
("message",
function(event) {
var message = event.data;
var domain = event.origin;
var targetWindow = window.opener; // The Window interface's opener property returns a reference to the window that opened the window. In other words, if window A opens window B, B.opener returns A.
console.log("Message: " + message + ", Domain: " + domain + ", Opener: " + targetWindow),
}
)
</script>
</body>
</html>
当我单击该按钮时,会出现新窗口。在此之后,我通过按 F12 按钮访问 Web 开发人员 Firefox 工具,但不知何故,该消息未显示在控制台中。
如果有人可以帮助我解决这个问题,我将不胜感激。
亲切的问候。