admin管理员组文章数量:1427451
I cannot get any data if I use different domains. If I run it on the same server, no problem with it - message is obtained.
index.html:
<head>
<title>Test 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function popupResize() {
var popup = window.open('popup.html', '', 'resizable=yes,scrollbars=yes,width=500,height=500');
// var popup = window.open('https://foo/index.html', '', 'resizable=yes,scrollbars=yes,width=500,height=500');
window.addEventListener(
'message',
function(event) {
console.log(event.data);
},
false
);
}
</script>
</head>
<body>
<a href='javascript:void(0)' onClick="popupResize(); return false;">Go!</a>
</body>
popup.html:
<head>
<title>Game history details</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script>
function postSize() {
var h = 100;
var w = 200;
opener.postMessage([h, w], '*');
}
</script>
</head>
<body onload="postSize();">
test 1
</body>
How to get it working using different servers?
I cannot get any data if I use different domains. If I run it on the same server, no problem with it - message is obtained.
index.html:
<head>
<title>Test 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function popupResize() {
var popup = window.open('popup.html', '', 'resizable=yes,scrollbars=yes,width=500,height=500');
// var popup = window.open('https://foo/index.html', '', 'resizable=yes,scrollbars=yes,width=500,height=500');
window.addEventListener(
'message',
function(event) {
console.log(event.data);
},
false
);
}
</script>
</head>
<body>
<a href='javascript:void(0)' onClick="popupResize(); return false;">Go!</a>
</body>
popup.html:
<head>
<title>Game history details</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script>
function postSize() {
var h = 100;
var w = 200;
opener.postMessage([h, w], '*');
}
</script>
</head>
<body onload="postSize();">
test 1
</body>
How to get it working using different servers?
Share Improve this question edited Jan 12, 2020 at 9:42 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 14, 2015 at 15:10 Roman NewazaRoman Newaza 11.7k12 gold badges63 silver badges94 bronze badges 4- are you receiving any errors in the console? – David Commented Aug 14, 2015 at 15:11
- 2 no, no errors at all – Roman Newaza Commented Aug 14, 2015 at 15:16
- are the files in different relative paths on the different servers – David Commented Aug 14, 2015 at 15:19
- on local: popup.html, remote: foo/index.html – Roman Newaza Commented Aug 14, 2015 at 15:20
2 Answers
Reset to default 4Problem 1
popup.addEventListener(
You need to listen for the event on your original window, not on the popup. The popup is where the message es from, not where it is sent.
Use window.addEventListener(
or just addEventListener(
.
Problem 2
{h, w}
ES6 shorthand property names are not supported by IE, Opera, Safari or Android Mobile. They are best avoided.
Problem 3
parent.postMessage({h, w}, '*');
You are sending a message to the opening window, not the parent frame. There is no parent frame (so parent
recurses onto window
).
That should be:
opener.postMessage({h: h, w: w}, '*');
Problem 4
<script type="text/javascript"> var popup = window.open('popup.html', '', 'resizable=yes,scrollbars=yes,width=500,height=500');
Your script does not have permission to open a new window except in response to a user event. That code needs moving into a function and then called from, for instance, a click event.
If I run it on the same server, no problem with it - message is obtained.
It is the bination of problems 1 and 3 that cause this. You are binding an event handler to the popup (which you can only do from the opening window if it is on the same origin) and you are posting a message from the popup to itself (because parent === window
).
Complete, albeit not best practise, code that works in my tests:
http://localhost:7007/index.html
<!DOCTYPE html>
<html>
<script>
addEventListener("message", function (event) {
document.body.appendChild(document.createTextNode(event.data));
});
function pop() {
window.open("http://127.0.0.1:7007/popup.html");
}
</script>
<input type="button" onclick="pop()">
http://127.0.0.1:7007/popup.html
<!DOCTYPE html>
<html>
<script>
opener.postMessage([123, 456], '*');
</script>
<h1>popup</h1>
The eventListener should be attached to window
instead of popup
:
window.addEventListener(
'message',
function (event) {
console.log(event.data);
},
false
);
In your child window (popup), you are posting a message to the parent window, but the eventListener is attached to the child window instead.
本文标签: javascriptWindowpostMessage() issueStack Overflow
版权声明:本文标题:javascript - Window.postMessage() issue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745501136a2661037.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论