Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97472 Viewers
61 Video lessons · 84700 Viewers
71 Video lessons · 68900 Viewers
56 Video lessons · 101346 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.
Your file was successfully uploaded.