New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Start learning with our library of video tutorials taught by experts. Get started

HTML5: Messaging and Communications in Depth
Illustration by

Sending messages to a listener


From:

HTML5: Messaging and Communications in Depth

with Bill Weinman

Video: Sending messages to a listener

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.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
HTML5: Messaging and Communications in Depth
43m 19s Intermediate Jan 23, 2012

Viewers: in countries Watching now:

This course shows how to communicate between web pages, both within a single domain and across one or more domains, using the HTML5 Messaging API. Author Bill Weinman reviews security and the same origin policy, details cross-origin scripting techniques, and explores examples of cross-document messaging. The course describes how to register and send messages to listeners and handle errors.

Subjects:
Developer Web Web Development
Software:
HTML
Author:
Bill Weinman

Sending messages to a listener

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.

There are currently no FAQs about HTML5: Messaging and Communications in Depth.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed HTML5: Messaging and Communications in Depth.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.