Introduction
In this article, you will see how to let two browser windows communicate with each other.
Through this article, I will show you how to show the text written in one browser window to another browser window. You will see that we can chat within two browser windows.
In this article, we will mainly use JavaScript and jQuery.
Step 1
First of all, add two HTML pages to your Visual Studio.
Rename the first of the HTML pages to MainPage.htm and the other one to RecieverPage.htm.
We will first work on the MainPage.htm, this will be the page that will first run and will help us to open the second browser window.

Step 2
After adding the page the first thing to do is to add a jQuery file named "jquery.windowmsg-1.0.js". You can add this file by downloading the Zip file provided previously.
On the MainPage.htm we will add two TextBoxes and three Buttons. The Textboxes will be used to communicate within the browsers and the buttons will be used to open the receiver window and to send the message.
It's code will be like this:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <title></title>
- <script src="http://jqueryjs.googlecode.com/files/jquery-1.2.3.min.js" type="text/javascript"></script>
- <script src="jquery.windowmsg-1.0.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(function() {
- $.initWindowMsg();
- var secondwindow;
- $('#clickbutton').click(function() {
- secondwindow = window.open('Recieverpage.htm', 'child', "width=400, height=400, location=no, menubar=no, scrollbars=no, status=no, toolbar=no");
- if (window.focus) { secondwindow.focus() }
- });
- $('#sendbutton').click(function() {
- value = $('#textbox1').val();
- $.triggerWindowEvent(secondwindow, "parentMsg1", value);
- });
- $('#sendbutton2').click(function() {
- value = $('#textbox2').val();
- $.triggerWindowEvent(secondwindow, "parentMsg2", value);
- });
- $.windowMsg("childMsg1", function(message) {
- $('#textbox1').val(message);
- });
- $.windowMsg("childMsg2", function(message) {
- $('#textbox2').val(message);
- });
- });
- </script>
- </head>
- <body>
- <h1>
- This is Main Window
- </h1>
- <p>
- Click on the Button to open another Window and then start typing your message in it.
- </p>
- <form>
- <input id="clickbutton" type="button" value="Click to Open Window"></input>
- <p>
- <input id="textbox1" type="text" value=""></input>
- <input id="sendbutton" type="button" value="send"></input>
- </p>
- <p>
- <input id="textbox2" type="text" value=""></input>
- <input id="sendbutton2" type="button" value="send"></input>
- </p>
- </form>
- <form name="newform">
- <input type="hidden" name="newformEvent" value="childMsg2"></input>
- <input type="hidden" name="newformData" value="hi"></input>
- <input id="button3" type="button" style="display:none" value="" name="newformButton"></input>
- </form>
- </body>
- </html>





Comments
Join the conversation! Your thoughts help the community grow.