Skip to content

Instantly share code, notes, and snippets.

@orzFly
Created February 6, 2018 08:21
Show Gist options
  • Save orzFly/aee58e317e42e327b700abb1766ada1a to your computer and use it in GitHub Desktop.
Save orzFly/aee58e317e42e327b700abb1766ada1a to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/zeyihiw
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class='origin'></div>
<div class='message'></div>
<button onclick="fire()">Submit</button>
<script id="jsbin-javascript">
"use strict";
window.myEvent = undefined;
function fire() {
window.myEvent.postMessage({ $kawori: "submit", payload: { "a": prompt() } }, event.origin);
}
function receiveMessage(event) {
if (event.data && event.data.$kawori === 'init') {
document.querySelector('.origin').innerText = event.origin;
document.querySelector('.message').innerText = JSON.stringify(event.data);
window.myEvent = event;
}
}
window.addEventListener("message", receiveMessage, false);
if (window.parent == window) {
document.querySelector('.message').innerText = "唉,你直接访问这个页面是没有意义的。";
} else {
window.parent.postMessage({ $kawori: "init" }, "*");
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">window.myEvent = undefined;
function fire() {
window.myEvent.postMessage({ $kawori: "submit", payload: { "a": prompt() } }, event.origin);
}
function receiveMessage(event)
{
if (event.data && event.data.$kawori === 'init') {
document.querySelector('.origin').innerText = event.origin;
document.querySelector('.message').innerText = JSON.stringify(event.data);
window.myEvent = event;
}
}
window.addEventListener("message", receiveMessage, false);
if (window.parent == window) {
document.querySelector('.message').innerText = "唉,你直接访问这个页面是没有意义的。"
} else {
window.parent.postMessage({ $kawori: "init" }, "*");
}</script></body>
</html>
"use strict";
window.myEvent = undefined;
function fire() {
window.myEvent.postMessage({ $kawori: "submit", payload: { "a": prompt() } }, event.origin);
}
function receiveMessage(event) {
if (event.data && event.data.$kawori === 'init') {
document.querySelector('.origin').innerText = event.origin;
document.querySelector('.message').innerText = JSON.stringify(event.data);
window.myEvent = event;
}
}
window.addEventListener("message", receiveMessage, false);
if (window.parent == window) {
document.querySelector('.message').innerText = "唉,你直接访问这个页面是没有意义的。";
} else {
window.parent.postMessage({ $kawori: "init" }, "*");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment