Home Web Front-end H5 Tutorial How Storage Event implements communication between pages

How Storage Event implements communication between pages

Jul 24, 2018 am 09:58 AM
html5 javascript front end

The content shared with you in this article is about how Storage Event implements communication between pages. The content is of great reference value and I hope it can help friends in need.

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

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

  2. 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 Pages under the same domain name, and execute the window.localStorage.setItem method on any one of them (also ensure that the second condition mentioned at the beginning of the article is met), then if other pages listen to the onstorage event, Then the onstorage event callbacks in these pages 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('a', 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, it is required here (difference between windows and tab pages), 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 does this communication mean to us? What's the purpose?
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:

  1. key is initialized or updated The key name of storage

  2. oldValue is the value before the storage is initialized or updated

  3. 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

Related recommendations:

Android custom ring LoadingView effect

Html5 mobile terminal award-winning seamless scrolling animation implementation

The above is the detailed content of How Storage Event implements communication between pages. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles