I've an HTML page (file main.html) opening a new tab in the same domain using JavaScript with the window.open("newtab.html") method.
In the new tab users do something ending their activity clicking a button. At this point, I would like to send a message to the opener window. I tried with postMessage, but from a new tab I can't have a reference to the opener.
From the new tab I'd like something like, but I've "ko"
var w = window.opener;
if (w) {
w.postMessage("hi", "http://10.150.10.43");
} else {
alert("ko");
}
What is the best way to send message from the secondary tab/window to the main one (in the same domain)?
I've an HTML page (file main.html) opening a new tab in the same domain using JavaScript with the window.open("newtab.html") method.
In the new tab users do something ending their activity clicking a button. At this point, I would like to send a message to the opener window. I tried with postMessage, but from a new tab I can't have a reference to the opener.
From the new tab I'd like something like, but I've "ko"
var w = window.opener;
if (w) {
w.postMessage("hi", "http://10.150.10.43");
} else {
alert("ko");
}
What is the best way to send message from the secondary tab/window to the main one (in the same domain)?
Share Improve this question edited Jul 24, 2021 at 17:50 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Aug 24, 2012 at 10:14 Luca RasconiLuca Rasconi 1,1051 gold badge11 silver badges33 bronze badges 4- possible duplicate of Can I control two browser windows with one HTML5 app? – josh3736 Commented Jul 26, 2014 at 0:21
- 1 Hey Luca, don't forget to mark the answer! Ivan deserves the rep points. :-) – Ionică Bizău Commented Oct 6, 2015 at 7:49
- Yes, you're right :) – Luca Rasconi Commented Oct 6, 2015 at 11:46
- Possible duplicate of Communication between tabs or windows – Michał Perłakowski Commented Dec 13, 2016 at 0:17
1 Answer
Reset to default 15That is strange. Opening a new window with window.open("newtab.html")
should give the newly opened window the ability to reference the opener via window.opener
.
Anyway, there are several other options for municating between two windows on the same domain. I think that the following two are the easiest to implement:
Use localStorage. If you store some data under some key into localStorage in the new tab window, then the opener window will get a storage event. If you catch the event with a handler, you can read the data from localStorage that you wrote from the new tab window. Note that events are fired only if both pages are on the same domain, and if there are actually two windows in question (events are not fired within the same window that wrote data). For more info on how to do this -- see for example: http://diveintohtml5.info/storage.html
Use shared Web workers. A web worker is a JS script that is executed in the background on a separate thread. Shared web workers are a special type of Web workers that allow any number of parent documents to municate with a single worker. So, you could use the worker to relay messages between the opener window and the new tab window. The API for municating with a workers is very similar to the postMessage API. For more info on how to do this -- see for example: http://www.sitepoint./javascript-shared-web-workers-html5/.