Skip to content
Advertisement

Is there any method to pass live data from one Browser tab another Browser tab by using Javascript

I created an increment and decrement program but in that, I want to pass data from 1st Browser to 2nd Browser(one tab to another tab).

In Index.html there are two buttons for incrementing and decrementing operations. On the 2nd page only there data will show but my code is working on only same tab. as well as How I can store data after page refresh?

index.html

JavaScript

index.css

JavaScript

index.js

JavaScript

2nd Page Name as Showdata

Showdata.html

JavaScript

showdata.js

JavaScript

Can anyone please provide a solution for my problem?

Advertisement

Answer

One (of several) approaches you could use would be to use the BroadcastChannel api. The blurb from MDN states:

The BroadcastChannel interface represents a named channel that any browsing context of a given origin can subscribe to. It allows communication between different documents (in different windows, tabs, frames or iframes) of the same origin. Messages are broadcasted via a message event fired at all BroadcastChannel objects listening to the channel.

The index page that has the two buttons:

JavaScript

And the listening page that is updated in real-time when a button is clicked.

JavaScript

The index page establishes a channel to which the listener must also subscribe and sends a simple message ( the datatype is quite flexible ) which is processed in the second window/tab

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement