Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
Skill Level Intermediate
Sending messages to a listener is simply a matter of calling the postMessage method on the target window object. So here I have a working copy of sender-one-start, and I named it sender-one-working.html, and this is in the Chap02 folder of the exercise files on my server. And over here I have sender-two-working.html, which is a working copy of sender-two-start.html and I'm also editing that on my server.
So the first thing I'm going to do is I'm going to page through this, for those of you who are typing it in all. This is actually the place where we left off in the last exercise, so if you did the last exercise, you already have this file. So that's the sender-one, and this is sender-two. I'm just noticing that this time function goes off the end of the screen there just a little bit. So you can see what that is. It's just got one little character off the end of the screen there.
So now the next thing we want to do is we want to make sure that we have these two variable correct, the origin and the URL. So in sender-one on line 35 and 36, you want to make sure that these match exactly. And you notice here it says listener-two-working, because that's left over from the last time, so I'm going to type in here sender-two-working. And the rest of this is correct, so I'm going to press Command+S to save and then look over at sender-two and do the same thing here on line 21. You just want to make sure that that is the origin for your opposite file.
So while we're in sender-two, we're going to ahead--because sender-two is the first one that actually gets ready to send a message, because you remember, sender-one loads up sender-two, and so sender-one doesn't actually have a window object for sender-two until sender-two sends it a message. The first thing that sender-two has to do is it needs to send a message to sender-one to let it know that it finished loading. So I'm going to come down here in init. I'm actually going to start with a comment and say, "send a message to the parent to let it know that windowTwo has finished loading." And so I'm going to make a local copy of variable for windowOne because I'm not actually going to use this outside of here; otherwise, I could make it global.
There wouldn't be any harm in it. And I'm going to assign it to that parent. Because we have a parent object already--this got loaded up by the parent--so we can just load parent and get that window object as easily as that. And then I can say windowOne.postMessage--it's really that simple--and give it the message and originOne. And so in the message here I'm just going to say, "this is from windowTwo!" And then we'll log it that we've done something.
You know when I'm developing or experimenting or learning about something, I like to do a lot of logging, and it tells me exactly what's going on and in what sequence, and it helps me to keep track of what I'm doing and what I'm learning. "message sent to windowOne." All right, so I'm going to press Command+S to save this on the Mac, and we're going to go ahead and run it. Now we've only changed sender-two. We have not changed sender-one, except to make it load up the right thing, but I'm still going to start with loading up sender-one-working, because that loads the frame for sender-two.
So I'm going to click on sender-one-working, and there we go. The outer one is sender-one, and the inner one is sender-two. And you notice here it says, "message sent to sender-one." As soon as it loads up, it sends a message to sender-one, and sender-one has this message from origin, and there is the origin of two.3sn.net, and this is from windowTwo. So windowOne loads up windowTwo. windowTwo loads up, and the first thing it does is it sends a message back to windowOne to let it know where it is, and now windowOne actually has a window object for windowTwo. It didn't have that before.
So now we can go back here into windowOne, sender-one-working.html, and here on our handleMessage function, you see where we have the message from origin, and that's this message right here. And there is the origin where it came from, two.3sn.net. So that's working. And we log the event data, and that's this message here, "this is from windowTwo." And so now what we can do, you see this message here that says, "if not windowTwo, windowTwo = event.source," so we did not have a windowTwo before-- remember, it's initialized to null--and so now we have it, because event.source gives us that windowTwo object.
So now we can just come down here, we can say windowTwo.postMessage (this is from windowOne! and originTwo). You have to give it the origin of the object that you're sending it to. And then we'll log a message, and we'll save that with Command+S. Now we're going to go ahead and we're going to send a message back to windowTwo. So I'm going to press reload up here, and there we have it, "message send back to windowTwo." And then windowTwo has a message from origin, and there's the one.3sn.net, and the message itself, "this is from windowOne." So now we're successfully sending messages from windowTwo to windowOne and then back from windowOne to windowTwo.
We'll take a look at a more interactive example later in the course.