Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Communicate between two browser windows?

Tags:

javascript

I have parent browser window P . on clicking a button a new browser window WIN-A is opened. then again pressing the same button it should read the title of the WIN-A window and open WIN-B

how to achieve this using Javascript?

Thanks in advance

like image 501
praveen Avatar asked Dec 02 '09 20:12

praveen


2 Answers

Given:

var myWindow = open("foo.bar");

Old method: Change the window object's name property:

myWindow.name = "...";
// in foo.bar:
setInterval(someFunctionToCheckForChangesInName, 100);

HTML5 method: Call the window object's postMessage method:

myWindow.postMessage("...", "*");
// in foo.bar:
(window.addEventListener || window.attachEvent)(
  (window.attachEvent && "on" || "") + "message", function (evt) {
    var data = evt.data; // this is the data sent to the window
    // do stuff with data
  },
false);
like image 157
Eli Grey Avatar answered Oct 22 '22 06:10

Eli Grey


A similar question was just asked recently:

Stack Overflow question: Quickest way to pass data to a popup window I created using window.open()?

Using that as a base (and provided Parent and WIN-A are on the same domain):

// Put outside click handler
var winA = null;

// Put inside click handler

if(!winA){
    // Store the return of the `open` command in a variable
    var winA = window.open('http://www.mydomain.com/page1');
} else {
    var title = winA.document.title;
    // Do something with title
    var winB = window.open('http://www.mydomain.com/page2');
}
like image 36
Doug Neiner Avatar answered Oct 22 '22 05:10

Doug Neiner