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

HTML5: Messaging and Communications in Depth

Exploring the example code


From:

HTML5: Messaging and Communications in Depth

with Bill Weinman

Video: Exploring the example code

So this is our little chat application, and you can see that I can type in a message here, "hi there," and it comes up over there. And I can type in a message here and it says, "hi back," and that one comes up over here. And these are in two different domains, which means that they're in two different origins for the purposes of the same-origin policy. So let's take a look at how this works and how this is implemented. Here we have chat-one.html, and this is the one that's running on one.3sn.net, and here we have chat-two.html, which is running on two.3sn.net.

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

Exploring the example code

So this is our little chat application, and you can see that I can type in a message here, "hi there," and it comes up over there. And I can type in a message here and it says, "hi back," and that one comes up over here. And these are in two different domains, which means that they're in two different origins for the purposes of the same-origin policy. So let's take a look at how this works and how this is implemented. Here we have chat-one.html, and this is the one that's running on one.3sn.net, and here we have chat-two.html, which is running on two.3sn.net.

So before we move forward, let's just take a quick browse through it for the people that are typing along with us. So that was chat-one.html, and now we'll look at chat-two.html. And the time-string gets cut off there just a little bit. There it is. And there we have it. So the first thing you want to do is you want to come down here in both of these and make sure that your URLs and origin strings are correct for your server.

These are correct for my server here on 36 and 37 in the chat-one, and in chat-two it's right there on line 24. So you want to make sure that those are correct for your server. So there are a couple of things that are different here and that make this work. First of course we have the init function, which gets called by onload, way down there on line 131. And in chat-one really all its doing is setting up the event handlers, loading the frame, and selecting the chat text box.

And so if we come down here into the HTML, you notice that we just have this little form with an onSubmit, so it doesn't have a Submit button. All you do is press Enter on the one field. And it's got this one text field, and it is called chatText. So up here in the init function you see it gets that element using the little element shortcut that I've got down here in my Utilities. It gets that element and it calls select on it, and that's what gives it, when we load this page up, you notice the first thing it does is it selects whatever text is in that box and it puts the cursor there.

Other than that, it's just waiting for a message to come in. And when a message does come in, it calls this handleMessage event handler, and you'll notice that it calls JSON.parse on the event data. And the reason for this is that whenever we send a message--notice here is our sendChat function--when we send a message we call sendObject, and sendObject actually takes that message and it calls stringify on the JSON, so that it takes that message it, converts it to JSON, and then it post it as a text string.

And the reason for this of course is so that it will work in Internet Explorer. So that adds a little bit of complexity. chat-two is almost exactly the same. The main difference being in the init. Because when the init gets called, it can grab that parent object and call it windowOne, and it calls sendObject again so that it gets JSON-stringified to send that windowTwo loaded message we saw over here info windowTwo loaded.

And so all of the messages are being sent as structured data. So this message is being sent as an info-type message, and the chat messages are being sent as chat-type messages. And all of that gets stringified in sendObject. So really, those are the only significant differences between this and the applications that we've seen earlier, and so it makes it really simple to send arbitrary data back and forth between two windows in different origins.

So you can see that the HTML5 messaging API is simple enough that it can get out of your way and allow you to get creative with your applications. Obviously this is a contrived application designed to demonstrate the features of the interface, but hopefully you can see how easy it is to implement cross- origin scripting with the HTML5 messaging API, and now you can use it in your own real-world applications.

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

Share a link to this course
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.

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
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



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.

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