praveen
praveen

Reputation: 71

How to Communicate between two browser windows?

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

Upvotes: 7

Views: 6618

Answers (3)

Gabriel McAdams
Gabriel McAdams

Reputation: 58261

When you call window.open, it returns a reference to the newly opened window. You can store the references to the windows you open in an array, and then iterate that array when you need to open another new window.

Gabriel

Upvotes: 0

Eli Grey
Eli Grey

Reputation: 35850

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);

Upvotes: 6

Doug Neiner
Doug Neiner

Reputation: 66191

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');
}

Upvotes: 3

Related Questions