Skip to content

Instantly share code, notes, and snippets.

@mehmetron
Created August 15, 2021 11:12
Show Gist options
  • Save mehmetron/7b422029e46ce9d4df7032d2768c8b8b to your computer and use it in GitHub Desktop.
Save mehmetron/7b422029e46ce9d4df7032d2768c8b8b to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>A static page</title>
</head>
<body>
<h1>Hello from page 2</h1>
<script>
window.addEventListener("message", function(event) {
console.log("13 event ", event)
if (event.origin != 'http://localhost:3000') {
// something from an unknown domain, let's ignore it
return;
}
console.log("18 I think I'm in love ", event.data );
// alert( "received: " + event.data );
if (event.data == "back") {
window.history.back()
} else if (event.data == "forward") {
window.history.forward();
}
// can message back using event.source.postMessage(...)
});
</script>
</body>
</html>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>A static page</title>
</head>
<body>
<h1>Hello from page 1</h1>
<!-- <form id="form">
<input type="text" placeholder="Enter message" name="message">
<input type="submit" value="Click to send">
</form> -->
<!-- <button id="send">Send Message</button> -->
<a href="https://blog.teamtreehouse.com/cross-domain-messaging-with-postmessage">info</a>
<button id="back">Back</button>
<button id="forward">Forward</button>
<iframe
src="http://localhost:3001/"
name="example"
id="iframe"
style="display:block;height:200px;width:400px"
></iframe>
<script>
// let win = window.frames.example;
// win.postMessage("message", "http://localhost:3001/");
// form.onsubmit = function() {
// var receiver = document.getElementById('iframe').contentWindow;
// console.log("28 submit ", this.message.value)
// receiver.postMessage(this.message.value, 'http://localhost:3001/');
// return false;
// };
// Get the window displayed in the iframe.
var receiver = document.getElementById('iframe').contentWindow;
// Get a reference to the 'Send Message' button.
// var btn = document.getElementById('send');
var back = document.getElementById('back');
var forward = document.getElementById('forward');
// A function to handle sending messages.
function goForward(e) {
// Prevent any default browser behaviour.
e.preventDefault();
// Send a message with the text 'Hello Treehouse!' to the new window.
receiver.postMessage('forward', 'http://localhost:3001');
}
// A function to handle sending messages.
function goBack(e) {
// Prevent any default browser behaviour.
e.preventDefault();
// Send a message with the text 'Hello Treehouse!' to the new window.
receiver.postMessage('back', 'http://localhost:3001');
}
// Add an event listener that will execute the sendMessage() function
// when the send button is clicked.
// btn.addEventListener('click', sendMessage);
forward.addEventListener('click', goForward);
back.addEventListener('click', goBack);
</script>
</body>
</html>
package main
import (
"log"
"net/http"
)
func main() {
fs := http.FileServer(http.Dir("./static"))
http.Handle("/", fs)
log.Println("Listening on :3001...")
err := http.ListenAndServe(":3001", nil)
if err != nil {
log.Fatal(err)
}
}
package main
import (
"log"
"net/http"
)
func main() {
fs := http.FileServer(http.Dir("./static"))
http.Handle("/", fs)
log.Println("Listening on :3000...")
err := http.ListenAndServe(":3000", nil)
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment