Communicate Between Two Tabs of a Browser


In this article, you will see how to let two tabs in a browser communicate with each other.
Using this article I will show you how text written on one page of a browser can be automatically updated whenever you change the text on another page.
In this article, we will mainly use the JavaScript and Cookies of the browser.
Step 1
First of all, add two HTML pages to your Visual Studio.
Rename the first of the HTML pages to Transfer and the other one to Collector.
We will first work on the Transfer Page, this will be the page where we will write our message.
Step 2
In the Transfer Page, we will use a TextBox and a Button. The TextBox will the area where we will write our message and the button will be used to reset all the entered information.
On this page, we will create two functions named setCookie and reset information. These functions will help to set the values of the cookies and to reset the TextBox on the click of the button.
Its complete information will be like this:
  1. <h1>Information Transfer</h1>  
  2. <p>  
  3. Write into the TextBox and after that go to the second page  
  4.    </p>  
  6. <form name="transfer">  
  7. <input type="text" name="sendinformation" size="30" value="">  
  8. <input type="reset" value="Reset">  
  9. </form>  
  11. <script type="text/javascript"><!--  
  12.     function setCookie(value) {  
  13.         document.cookie = "cookie-msg-test=" + value + "; path=/";  
  14.         return true;  
  15.     }  
  16.     function resetinformation() {  
  17.         var res = document.forms['transfer'].elements['sendinformation'];  
  18.         setCookie(res.value);  
  19.         setTimeout(resetinformation, 100);  
  20.     }  
  21.     resetinformation();  
  22. //--></script> 
Step 3
Now we will work on the second page which is "Collector".
This page will receive the message sent from the first page. On this page, we will just have a TextBox that will work as a receiver of the message.
On this page again we will create two the functions "getCookie" and "resetinformation". getCookie will be used to get the Cookies and resetinformation will be used to reset the value of TextBox.
Its complete code will be like this:
  1. <h1>Information Collector</h1>  
  3. <p>Watch the text appear in the text box below as you type it in the sender.</p>  
  5. <form name="collectmessage">  
  6. <input type="text" name="collectnewmessage" size="30" value="" readonly disabled>  
  7. </form>  
  9. <script type="text/javascript"><!--  
  10.     function getCookie() {  
  11.         var y = "cookie-msg-test=";  
  12.         var spl = document.cookie.split(';');  
  13.         for (var z = 0; z < spl.length; z++) {  
  14.             var x = spl[z];  
  15.             while (x.charAt(0) == ' ') x = x.substring(1, x.length);  
  16.             if (x.indexOf(y) == 0) {  
  17.                 return x.substring(y.length, x.length);  
  18.             }  
  19.         }  
  20.         return null;  
  21.     }  
  22.     function resetinformation() {  
  23.         var text = getCookie();  
  24.         document.forms['collectmessage'].elements['collectnewmessage'].value = text;  
  25.         setTimeout(resetinformation, 100);  
  26.     }  
  27.     resetinformation();  
  28. //--></script> 
Step 4
Now when we run these pages you will get the output something like this:
Initially, Sender is empty so the Receiver is also empty:
Enter Something in TextBox
When we enter something in the sender then the receiver automatically receives it.
Reset Button
If you reset the First Page by clicking on the Reset Button then you can see the second page is reset automatically.