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

The difference between char and varchar in MYSQL

CHAR and VARCHAR types are similar, differing pri...

React Hooks Usage Examples

Table of contents A simple component example More...

Example of using Nginx to implement port forwarding TCP proxy

Table of contents Demand Background Why use Nginx...

Node implements search box for fuzzy query

This article example shares the specific code for...

A time-consuming troubleshooting process record of a docker error

Table of contents origin Environmental Informatio...

How to deal with garbled characters in Mysql database

In MySQL, database garbled characters can general...

HTML tags list and usage instructions

List of HTML tags mark type Name or meaning effec...

Introduction to the use of alt and title attributes of HTML img tags

When browser vendors bend the standards and take i...

Detailed explanation of JavaScript primitive data type Symbol

Table of contents Introduction Description Naming...

Practical record of MySQL 5.6 master-slave error reporting

1. Problem symptoms Version: MySQL 5.6, using the...

JavaScript canvas to achieve meteor effects

This article shares the specific code for JavaScr...

Example code for implementing raindrop animation effect with CSS

Glass Windows What we are going to achieve today ...

Analyze the problem of pulling down the Oracle 11g image configuration in Docker

1. Pull the image docker pull registry.cn-hangzho...

Centos7 installation of MySQL8 tutorial

MySQL 8 new features: My personal opinion on MySQ...