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

HTML5 Power Workshop

Getting started with cross-domain messaging


From:

HTML5 Power Workshop

with Andy Olsen

Video: Getting started with cross-domain messaging

HTML5 has a communication API which allows web pages to talk to each other even if they've been downloaded from different web servers. Now, this is quite a powerful mechanism. You could create a client side mashup, where you have content being pulled in from lots of different service providers, such as the current weather or stock tick pricer. And you can do all that accumulation and aggregation client side, because you can now communicate with different web pages that have been downloaded, from different servers.

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 Power Workshop
3h 9m Intermediate May 31, 2012

Viewers: in countries Watching now:

In this course, part of his series of titles on HTML5 and CSS3, author and expert Andy Olsen looks at advanced topics like geolocation, mobile development, web sockets, Web SQL, and web workers. You'll also learn how to communicate between pages downloaded from different servers and how to use the new Ajax features in XMLHttpRequest Level 2. After completing this workshop, developers be well equipped to start utilizing the powerful features of HTML5.

Topics include:
  • Using the Communications API
  • Understanding geolocation
  • Getting and watching the current position
  • Using web workers and WebSockets
  • Implementing mobile web user interfaces
  • Managing data in a mobile web application
  • Working offline
  • Using Web SQL
  • Using drag-and-drop
Subjects:
Developer Web Web Design Web Development video2brain
Software:
HTML
Author:
Andy Olsen

Getting started with cross-domain messaging

HTML5 has a communication API which allows web pages to talk to each other even if they've been downloaded from different web servers. Now, this is quite a powerful mechanism. You could create a client side mashup, where you have content being pulled in from lots of different service providers, such as the current weather or stock tick pricer. And you can do all that accumulation and aggregation client side, because you can now communicate with different web pages that have been downloaded, from different servers.

So I'm going to show in this lesson how the mechanism works. There are three files involved. The first file is main window the HTML, and as you can see I've downloaded it from a domain called MyDomain 1. I'm going to deposit some money into a bank account. I'll deposit a 100 pounds, and then I'll deposit 200 pounds and then withdraw 50 pounds. So, there's a global variable on this page that keeps track of the balance.

Now, I can also click this new Window button. Look what happens when I click this button a few times. It creates a few child windows on the right hand side. I've done some maths, to make them look in a nice position. Each new window has been downloaded from a different domain, and that's the key point here. The child windows come from MyDomain 2. The main window comes from MyDomain 1. So normally, before HTML5, you wouldn't be able to talk back and forth across these pages, because of security restraints, but you can do that now in HTML5.

So, to illustrate, when I click the Broadcast button, it will post the message to each child window, telling it the new balance. So, I click the button, broadcast and as you can see, each of the child windows has received a message with the new balance and displayed the new balance on the page here. So, there's a function called post message, which allows the main window to post a message to each child window to tell it to do something. You can also post messages in the reverse direction using the same approach. When I click the Levy Fee button, it posts a message back to the main window to say, take 10 pounds off the bank balance, it's like a bank charge. So, when I click the button here, it will post the message back to the main window and there's an important point here.

Whenever you post a message either from the main window to the child window or from the child window back to the main window, the receiver always has the opportunity to see who sent the message. And you can decide whether you want to accept the message or reject it based on whether you trust the domain or not. This is called the origin, and the origin includes the schemer, suggests HTTP and the domain name such as MyDomain 2. And potentially the port number as well, so the origin identifies who sent the message.

In this case, the sender was MyDomain 2, and you get the chance in the receiver to decide whether to accept the message or reject the message. Now, in my code, I accept the message. So, it will have reduced the balance by 10 pounds. And automatically, it rebroadcasts the new balance to each of the child windows. So, the child windows now show a balance of 240 as opposed to 250, if I do it again. And you'll see the balance go from 240 down to 230.

For this example, to work I have to set my files up, so that they came from different web servers. On the on machine, if you have several different web servers, then the example is quite straight forward. Copy main window onto one server, and copy child window onto another. On my machine, I just have one web server, which is Microsoft IIS. And I've copied both of these files into the same folder, which is inetpub\wwwroot. I'll just show you what that folder looks like on my machine. So you can see main window and child window, are actually on the same web publishing folder plus the style sheet, that makes all the buttons look pretty.

So, what I had to do is I had to setup some aliases, so that MyDomain 1 and MyDomain 2 pretend to be different servers. The way you do that on Windows is in a file called hosts, here it is. I've opened it in the edit the hosts and is in this location c:windows\systems32\drivers\etc, that's a place where you can set up aliases for your web server. So at the end of the file, I added these two statements. I setup two aliases, MyDomain 1 is an alias for localhost 127.0.0.1 is localhost.

So, MyDomain 1 is localhost effectively, and MyDomain 2 also is localhost. But as far as the application is concerned they are different domains. There's one more important point I want to show in the example. I'm going to change my main window to be localhost instead of MyDomain 1 localhost. Okay, and I will just close the other child windows, that I have opened up earlier just avoid any potential confusion.

So now, I've downloaded the main window from localhost as supposed to MyDomain 1. So, I'll just deposit the money 200 pounds, lets make it 20,000 pounds deposit. I created some child windows, now watch what happens when I click the Broadcast button, sending it from localhost to these child windows over here on MyDomain 2. So, click the Broadcast button notice this, I've actually got some logic in the child windows it checks the sender and if the origin is anything other than MyDomain 1, it rejects it. So, this illustrates the opportunity that you have, as the receiver of messages, to check the origin of the sender, and you can decide whether you trust it or not. You can decide whether you want to accept the message or reject the message. A very powerful mechanism that's completely safe and that allows child windows to talk to each other, even if they've come from different domains, in a secure manner.

There are currently no FAQs about HTML5 Power Workshop.

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 Power Workshop.

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