This is what happened. Today I was playing with GitHub. I first browsed some pages without logging in, and then logged in on a certain page. When you switch to other pages, you will see the following prompt: So how is this done? We can think of one way is localStorage. When logging in to a certain page, modify the localStorage status. When other pages are displayed, read the latest status and then display a prompt: // Login page localStorage.setItem('login', true); // Other pages document.addEventListener("visibilitychange", function() { if (localStorage.setItem('login') === 'true') { alert('You are logged in, please refresh the page'); } } However, GitHub did not do this, and no relevant fields could be found in localStorage. After some searching, I found that they implemented it using sharedWorker. Then let's take a look at sharedworker What is sharedWorkerAs the name implies, sharedWorker is a type of worker that can be shared by all pages of the same origin. Just like the Worker API, you can register a sharedWorker instance by passing in the js URL: let myWorker = new SharedWorker('worker.js'); But it is different from ordinary Workers: Next, let's take a look at how messages are sent and received between the worker and the page. messagePortSuppose we have two js, one is page.js running in the page, and the other is worker.js running in the worker. Then we need to register a sharedWorker in page.js, the code is as follows: // page.js let myWorker = new SharedWorker('worker.js'); // page sends a message through the worker port myWorker.port.postMessage('hum'); // page receives messages through worker port myWorker.port.onmessage = (e) => console.log(e.data); // worker.js onconnect = function(e) { const port = e.ports[0]; port.postMessage('Hey'); port.onmessage = (e) => { console.log(e.data); } } Debugging sharedWorker In the above example, we used console.log in the worker to print the message from the page, so where can we see the printed log? We can enter `chrome://inspect in the browser address bar Here we see that our worker name is untitled, that's because the sharedworker constructor also supports passing in a second parameter as a name: let myWorker = new SharedWorker('worker.js', 'awesome worker'); Post messages on multiple pagesLet’s go back to the example at the beginning of the article. We have implemented communication between the page and the worker. So how do we let the worker send messages to multiple pages? One idea is to cache the port as a port pool, so that when we need to broadcast a message to all pages, we can traverse the port and send the message: // worker js const portPool = []; onconnect = function(e) { const port = e.ports[0]; // Add port to portPool when connecting portPool.push(port); port.postMessage('Hey'); port.onmessage = (e) => { console.log(e.data); } } function boardcast(message) { portPool.forEach(port => { port.portMessage(port); }) } In this way, we have basically realized the function of broadcasting messages to multiple pages. Clear invalid portsThere is a problem in the above implementation. After the page is closed, the port in the workerPool is not automatically cleared, resulting in a waste of memory. We can notify the shared worker that the page is about to be closed before the page is closed, and then have the worker remove the invalid messagePort from the portPool. // Page window.onbeforeunload = () => { myWorker.port.postMessage('TO BE CLOSED'); }; // worker.js const portPool = []; onconnect = function(e) { var port = e.ports[0]; portPool.push(port); port.onmessage = function(e) { console.log(e); if (e.data === 'TO BE CLOSED') { const index = ports.findIndex(p => p === port); portPool.splice(index, 1); } var workerResult = 'Result: ' + (e.data[0] * e.data[1]); port.postMessage(workerResult); } } function boardcast(message) { portPool.forEach(port => { port.portMessage(port); }) } In this way, we have implemented a simple sharedWorker for multi-page broadcasting. We can use it to broadcast the time: setInterval(() => boardcast(Date.now()), 1000); refer to https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker/SharedWorker This is the end of this article about sharedWorker in JavaScript to achieve multi-page communication. For more related js sharedWorker multi-page communication content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to connect a Linux virtual machine to WiFi
>>: CentOS installation mysql5.7 detailed tutorial
This article shares the specific code of Vue recu...
Shtml and asp are similar. In files named shtml, s...
Specific method: (Recommended tutorial: MySQL dat...
Use the Linux utility certbot to generate https c...
I have never been able to figure out whether the ...
Summarize 1. Similarities Both can change the int...
Table of contents Presentation Layer Business Lay...
Find the problem I recently encountered a problem...
Let's take a look at the command to restart t...
This article example shares the specific code of ...
Nowadays, tabs are widely used in web design, but...
Table of contents 1. rsync, cp copy files 2. sele...
0. Overview Zabbix is an extremely powerful ope...
A very common scenario in react projects: const [...
This tag is not part of HTML3.2 and only supports ...