Home Web Front-end H5 Tutorial Sample code for implementing communication between pages using Storage Events

Sample code for implementing communication between pages using Storage Events

Oct 09, 2018 pm 04:46 PM
event storage

This article mainly introduces the relevant information about the sample code of using Storage Event to realize communication between pages. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

We all know that triggering window.onstorage must meet the following two conditions:

  • Save (update) a certain storage through localStorage.setItem or sessionStorage.setItem

  • When saving (updating) this storage, its new value must be different from the previous value

The second condition above, simply speaking, is : Either it is the initialization of storage, because the storage does not exist, its value is null; or it is the update of existing storage

Example:

// 初始化storage
window.localStorage.setItem('a', 123);

// 注册onstorage事件
window.onstorage = (e) => {
  console.log(e);
};

// 更新storage
window.localStorage.setItem('a', 123);
Copy after login

The last line of code above will not trigger Onstorage event, because the value of a has not changed, it is 123 before and after, so the browser determines that this update is invalid

Since the onstorage event is triggered by the browser, if we open multiple same domain names pages under the page, and execute the window.localStorage.setItem method on any one of the pages (also ensure that the second condition mentioned at the beginning of the article is met), then if other pages listen to the onstorage event, the onstorage in these pages The event callback will be executed.

Example:

// http://www.example.com/a.html
<script>
// 注册onstorage事件
window.onstorage = (e) => {
  console.log(e);
};
</script>
Copy after login
// http://www.example.com/b.html
<script>
// 注册onstorage事件
window.onstorage = (e) => {
  console.log(e);
};
</script>
Copy after login
// http://www.example.com/c.html
<script>
// 触发onstorage事件
window.localStorage.setItem(&#39;a&#39;, new Date().getTime());
</script>
Copy after login

As long as page c is opened after page a and page b (even if the three pages are not in the same browser window, you need to distinguish between windows and tab pages) difference), then the onstorage events in pages a and b will be triggered

Now that we know how to use storage events to achieve communication between pages, what is the use of this communication for us?

In fact, we only need to know which storage update operation triggered the onstorage event. So how do we know? The onstorage event callback, like other event callback functions, also receives an event object parameter. There are three useful properties in this object, which are:

  • key is initialized or updated The key name of storage

  • oldValue is the value before the storage is initialized or updated

  • newValue is the value after the storage is initialized or updated

Combining these 3 key attributes, we can achieve data synchronization between pages

Finally, let’s mention the difference between localStorage and sessionStorage

What is stored in localStorage There is no expiration time setting for data, and the data stored in sessionStorage will be cleared when the page session ends

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more related tutorials, please visit Html5 Video Tutorial!

Related recommendations:

php public welfare training video tutorial

HTML5 graphic tutorial

HTML5Online Manual

The above is the detailed content of Sample code for implementing communication between pages using Storage Events. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Event processing library in PHP8.0: Event Event processing library in PHP8.0: Event May 14, 2023 pm 05:40 PM

Event processing library in PHP8.0: Event With the continuous development of the Internet, PHP, as a popular back-end programming language, is widely used in the development of various Web applications. In this process, the event-driven mechanism has become a very important part. The event processing library Event in PHP8.0 will provide us with a more efficient and flexible event processing method. What is event handling? Event handling is a very important concept in the development of web applications. Events can be any kind of user row

Steam Summer Sale - Valve teases 95% off AAA games, confirms discounts for viral games Palworld and Content Warning Steam Summer Sale - Valve teases 95% off AAA games, confirms discounts for viral games Palworld and Content Warning Jun 26, 2024 pm 03:40 PM

Steam's Summer Sale has previously played host to some of the best game discounts, and this year seems to be stacking up for another home run by Valve. A trailer (watch below) teasing some of the Steam Summer Sale discounted games was just released i

Where is the storage folder? Where is the storage folder? Jan 12, 2021 pm 02:02 PM

The storage folder is in file management. How to find it: 1. Directly open the mobile phone desktop and click System Tools to enter; 2. Select file management to jump; 3. Browse all files; 4. Find the storage folder in file management. .

How to use Pygame's Event event module in Python How to use Pygame's Event event module in Python May 18, 2023 am 11:58 AM

Pygame's Event module Event (Event) is one of the important modules of Pygame. It is the core of building the entire game program, such as commonly used mouse clicks, keyboard taps, game window movement, window resizing, triggering specific plots, and exiting. Games, etc., these can be regarded as "events". Event type Pygame defines a structure specifically used to process events, namely the event queue. This structure follows the basic principle of "first come, first processed" in the queue. Through the event queue, we can process user operations in an orderly and one-by-one manner ( trigger event). The following table lists the commonly used game events in Pygame: Name Description QUIT The user presses the close button of the window ATIVEEVENTPy

In JavaScript, when the browser window is resized, which event is this? In JavaScript, when the browser window is resized, which event is this? Sep 05, 2023 am 11:25 AM

Use the window.outerWidth and window.outerHeight events to get the window size in JavaScript when the browser resizes. Example You can try running the following code to check the browser window size using events −&lt;!DOCTYPEhtml&gt;&lt;html&gt; &lt;head&gt; &lt;script&gt;&am

Steam Summer Sale trailer teases 95% off AAA game deals, confirms price cuts for Palworld, Stellaris, Content Warning Steam Summer Sale trailer teases 95% off AAA game deals, confirms price cuts for Palworld, Stellaris, Content Warning Jun 26, 2024 am 06:30 AM

Steam's Summer Sale has previously played host to some of the best game discounts, and this year seems to be stacking up for another home run by Valve. A trailer (watch below) teasing some of the Steam Summer Sale discounted games was just released i

Explore the features and benefits of SessionStorage Explore the features and benefits of SessionStorage Jan 11, 2024 pm 03:16 PM

Introduction to SessionStorage: To understand its uses and advantages, specific code examples are required Introduction: In web development, we often need to store and manage user information and temporary data. To solve this problem, HTML5 introduces a new API: SessionStorage. This article will introduce the concepts, uses and advantages of SessionStorage, and give some specific code examples to help readers better understand it. 1. What is SessionStorage?

Tesla sends out Robotaxi invitations for October 10 autonomous driving demo event in LA Tesla sends out Robotaxi invitations for October 10 autonomous driving demo event in LA Sep 27, 2024 am 06:20 AM

It was initially expected that Tesla would unveil its previously leaked Robotaxi back in August of this year, but CEO Elon Musk postponed the event, citing aesthetic changes to the front of the robotaxi and additional time needed for a few last-minut

See all articles