Say I have two html pages and open them in two tabs. I'd like to make them communicate. As example when I click on a button on the first page, then it should call a function that does something on the second page.
function Test() { document.getElementById('test').innerHTML = "Test"; }
<!DOCTYPE html> <html> <head> <script src="index.js"></script> </head> <body> <button onclick="Test()">Click here</button> </body> </html>
And the second page:
<!DOCTYPE html> <html> <head> <script src="index.js"></script> </head> <body> <p id="test"></p> </body> </html>
When I click the button on the first page it should write Test
in the p
tag on the second page. They can use the same JavaScript file. But how can I achieve this?
You can't do this with just JavaScript. The point is: JS is a client-side language which means that it is downloaded by a client (a browser) and is run by it (not by server). For the 2 pages to communicate, you have establish the communication somehow, via some medium. The medium can be:
So, if you're interested in the third option, you should read about window.open
, window.opener
, window.parent
.
var newWindow = window.open(url, name, params);
will open a new window (say your second page) and bring you a variable newWindow
which is a reference to the window
object of the opened window. Try for instance
newWindow.write("haha, I'm controlling this stuff!");
Likewise, in the second window you can use
var oldWindow = window.opener;
There's also a number of methods you can use ( window``.close
, .moveBy
, .moveTo
, .resizeBy
, .resizeTo
etc etc).
Remember, however, that this interaction will be limited to your browser: if you change something as it is displayed in your browser (like add some text to a page) this won't affect the actual pages stored on your server because this requires your server do something via some server-side scripts.
PS to advance this technique, you may want to read about window.postMessage
but that's mostly designed for communication between pages that are cross-domain.
PPS Actually, there's more!
One thing to note is localStorage
and sessionStorage
have setItem
method which generates 'storage'
events on window
(try localStorage.setItem('key', 'value');
and window.addEventListener('storage', event => console.log(event.key));
).
Another, like Anderson Green has noted, is Broadcast Channel API (try const channel = new BroadcastChannel('my_channel')
, channel.postMessage('Hi there!')
and channel.addEventListener('message', event => console.log(event))
).
There's also SharedWorker
s and Service Workers .
Finally, you can use some off-the-shelve solutions like tabs-router , Hermes , Visibility , Duel and SE .
Those who speak Russian may also find more useful details in this article (wow!).
Try using cookies. It's the simplest way I can think of. This website might seem helpful: https://www.w3schools.com/js/js_cookies.asp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.