Skip to content

Commit

Permalink
📝 add postMessage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Copyes committed Mar 7, 2018
1 parent cd9263f commit 0427424
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion test-cross/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"scripts": {
"jsonp": "node ./jsonp/server.js & http-server ./jsonp",
"cors": "node ./cors/server.js & http-server ./cors",
"proxy": "node ./serverProxy/server.js"
"proxy": "node ./serverProxy/server.js",
"postMessage":
"http-server ./postMessage/client/ -p 8080 & http-server ./postMessage/server/ -p 8081"
},
"author": "lip.fan",
"license": "ISC",
Expand Down
24 changes: 24 additions & 0 deletions test-cross/postMessage/client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>postMessage</title>
</head>

<body>
<iframe src="http://localhost:8081/data.html" style='display: none;'></iframe>
<script>
window.onload = function () {
let targetOrigin = 'http://localhost:8081';
window.frames[0].postMessage('index.html 的 data!', targetOrigin);
}
window.addEventListener('message', function (e) {
console.log('index.html 接收到的消息:', e.data);
});
</script>
</body>

</html>
10 changes: 10 additions & 0 deletions test-cross/postMessage/server/data.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
window.addEventListener('message', function (e) {
if (e.source != window.parent) {
return;
}
let data = e.data;
console.log('data.html 接收到的消息:', data);
parent.postMessage('data.html 的 data!', e.origin);
});
</script>

0 comments on commit 0427424

Please sign in to comment.