How to use js to communicate between two html windows

How to use js to communicate between two html windows

Scenario: When page A opens page B, after operating on page B, page A needs to synchronize the changed data

Page A, http://127.0.0.1:10001/A.html

var domain = 'http://127.0.0.1:10001';

window.open('http://127.0.0.1:10001/B.html');
window.addEventListener('message', function (event) {
    if (event.origin !== domain) return;
    console.log('message received: ' + event.data, event);
}, false);

B page, http://127.0.0.1:10001/B.html, opener is the reference to the opener of the current window

var domain = 'http://127.0.0.1:10001';
window.opener.postMessage("success", domain);
window.close();

If A needs to open B and send data to B at the same time

// Sending data var domain = 'http://127.0.0.1:10001';
var myPopup = window.open('http://127.0.0.1:10001/B.html');
myPopup.postMessage('data', domain);

// Receive data window.addEventListener('message', function(event) {
    if(event.origin !== 'http://127.0.0.1:10001') return;
    console.log('message received: ' + event.data,event);
},false);

The above is the details of how to use js to communicate between two html windows. For more information about js communication between two html windows, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of JS homology strategy and CSRF
  • Detailed explanation of JavaScript homology policy and cross-domain access examples
  • Ajax and homology strategy implemented by JS (example explanation)
  • Detailed explanation of js homology strategy
  • EventBus in JavaScript implements communication between objects
  • A brief discussion on the pitfalls of real-time communication using vue websocket nodeJS
  • Detailed explanation of custom events for Vue.js component communication
  • SpringBoot implements jsonp cross-domain communication method example
  • Detailed code example of how Vue.js child components communicate with parent components
  • The process of using SockJS to implement webSocket communication in vue
  • How to implement same-origin communication in JavaScript

<<:  Analyze Mysql transactions and data consistency processing issues

>>:  Win10 DVWA download, installation and configuration graphic tutorial detailed explanation (novice learning penetration)

Recommend

Detailed explanation of Vue life cycle functions

Table of contents Lifecycle Functions Common life...

Getting Started with Mysql--sql execution process

Table of contents 1. Process 2. Core Architecture...

Apply provide and inject to refresh Vue page method

Table of contents Method 1: Call the function dir...

Summary of MySQL data migration

Table of contents Preface: 1. About data migratio...

Detailed explanation of client configuration for vue3+electron12+dll development

Table of contents Modify the repository source st...

HTML Tutorial: Collection of commonly used HTML tags (4)

Related articles: Beginners learn some HTML tags ...

HTML page native VIDEO tag hides the download button function

When writing a web project, I encountered an intr...

Summary of several common methods of JavaScript arrays

Table of contents 1. Introduction 2. filter() 3. ...

Mini Program to Implement Calculator Function

This article example shares the specific code of ...

How to implement real-time polygon refraction with threejs

Table of contents Preface Step 1: Setup and front...

Details about the like operator in MySQL

1. Introduction When filtering unknown or partial...

Native JS to implement click number game

Native JS implements the click number game for yo...

A brief discussion on the font settings in web pages

Setting the font for the entire site has always b...

Detailed process of SpringBoot integrating Docker

Table of contents 1. Demo Project 1.1 Interface P...