How to communicate between two browser windows?

I have a parent browser window P. When I click the button, a new WIN-A browser window opens. then pressing the same button again, he should read the window title WIN-A and open WIN-B

how to achieve this using javascript?

Thanks in advance

+6
javascript
source share
3 answers

Given:

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

Old method: change the name property of the window object:

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

HTML5 method: call the window 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); 
+4
source share

A similar question was recently asked:

Stack overflow question: The fastest way to pass data to a popup that I created using window.open ()?

Using this as a base (and the ones provided by Parent and WIN-A are in 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'); } 
+3
source share

When you call window.open, it returns a link to the newly opened window. You can save the links to the windows that you open in the array, and then iterate over this array when you need to open another new window.

Gabriel

0
source share

All Articles