


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);
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>
// http://www.example.com/b.html <script> // 注册onstorage事件 window.onstorage = (e) => { console.log(e); }; </script>
// http://www.example.com/c.html <script> // 触发onstorage事件 window.localStorage.setItem('a', new Date().getTime()); </script>
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
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!

PHP8.0中的事件处理库:Event随着互联网的不断发展,PHP作为一门流行的后台编程语言,被广泛应用于各种Web应用程序的开发中。在这个过程中,事件驱动机制成为了非常重要的一环。PHP8.0中的事件处理库Event将为我们提供一个更加高效和灵活的事件处理方式。什么是事件处理在Web应用程序的开发中,事件处理是一个非常重要的概念。事件可以是任何一种用户行

storage文件夹在文件管理中,其查找方法:1、直接打开手机桌面,点击系统工具进入;2、选择文件管理跳转;3、浏览全部文件;4、在文件管理中找到storage文件夹即可。

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

Pygame的Event事件模块事件(Event)是Pygame的重要模块之一,它是构建整个游戏程序的核心,比如常用的鼠标点击、键盘敲击、游戏窗口移动、调整窗口大小、触发特定的情节、退出游戏等,这些都可以看做是“事件”。事件类型Pygame定义了一个专门用来处理事件的结构,即事件队列,该结构遵循遵循队列“先到先处理”的基本原则,通过事件队列,我们可以有序的、逐一的处理用户的操作(触发事件)。下述表格列出了Pygame中常用的游戏事件:名称说明QUIT用户按下窗口的关闭按钮ATIVEEVENTPy

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

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

使用window.outerWidth和window.outerHeight事件在JavaScript中获取窗口大小,当浏览器调整大小时。示例您可以尝试运行以下代码来使用事件检查浏览器窗口大小−<!DOCTYPEhtml><html> <head> <script>&am

SessionStorage简介:了解它的用途和优势,需要具体代码示例引言:在Web开发中,我们经常需要存储和管理用户信息以及临时数据。为了解决这个问题,HTML5引入了一个新的API:SessionStorage。本文将介绍SessionStorage的概念、用途和优势,并给出一些具体的代码示例来帮助读者更好地理解它。一、什么是SessionStorage


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools
