Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Exploring the example code

From: HTML5: Messaging and Communications in Depth

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.

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.

Show transcript

This video is part of

Image for HTML5: Messaging and Communications in Depth
HTML5: Messaging and Communications in Depth

11 video lessons · 5736 viewers

Bill Weinman
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
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.


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 "Already a member? Log in

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

Your file was successfully uploaded.

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.