当前位置: 首页 > news >正文

postMessage

A:端口3000

import React, { useEffect } from 'react';function App() {useEffect(() => {const childWindow = document.getElementById('child').contentWindow;const sendMessageToChild = () => {childWindow.postMessage("主页面消息", "http://localhost:4000");};const receiveMessageFromChild = (event) => {if (event.origin === "http://localhost:4000" && typeof event.data === "string") {console.log(event);const messageElement = document.getElementById('message');if (messageElement) {messageElement.innerHTML = `收到 ${event.origin} 消息:${event.data}`;}event.source.postMessage("主页面收到消息并回复", event.origin);}};window.addEventListener('message', receiveMessageFromChild, false);sendMessageToChild();return () => {window.removeEventListener('message', receiveMessageFromChild);};}, []);return (<div className="App"><iframe id="child" src="http://localhost:4000"></iframe><div><h2>主页面跨域接收消息区域</h2><div id="message"></div></div></div>);
}export default App;

B:端口4000

import React, { useEffect } from 'react';function App() {useEffect(() => {const parentWindow = window.parent;const sendMessageToParent = () => {parentWindow.postMessage("子页面消息收到", 'http://localhost:3000');};const receiveMessageFromParent = (event) => {if (event.origin === "http://localhost:3000" && typeof event.data === "string") {console.log(event);const messageElement = document.getElementById('message');if (messageElement) {messageElement.innerHTML = `收到 ${event.origin} 消息:${event.data}`;}event.source.postMessage("子页面收到消息并回复", event.origin);}};window.addEventListener('message', receiveMessageFromParent, false);sendMessageToParent();return () => {window.removeEventListener('message', receiveMessageFromParent);};}, []);return (<div className="App">hello world<div id="message"></div></div>);
}export default App;

相关文章:

  • 聊一聊GPT——让我们的写作和翻译更高效
  • 如何设置没有采购申请不允许创建采购订单(TCODE:OMET)<转载>
  • 目标检测YOLO系列从入门到精通技术详解100篇-【目标检测】SLAM(补充篇)
  • 无效的标记: --release
  • 包装印刷行业万界星空科技云MES解决方案
  • eBPF BCC开源工具简介
  • 如何从视图中取消nspopover?
  • UUID 的 5 个版本
  • llinux的更目录下的文件作用和举例
  • SpringBoot+Swagger详细使用方法
  • wagtail的使用
  • 修复RGBA的png为RGB的png
  • idea 将分支的代码合并到master
  • 2023年【公路水运工程施工企业安全生产管理人员】免费试题及公路水运工程施工企业安全生产管理人员复审考试
  • 操作系统——文件在外存中的分配方式(王道视频p61 P62)
  • 【407天】跃迁之路——程序员高效学习方法论探索系列(实验阶段164-2018.03.19)...
  • C++入门教程(10):for 语句
  • CAP 一致性协议及应用解析
  • CentOS7 安装JDK
  • egg(89)--egg之redis的发布和订阅
  • Java 23种设计模式 之单例模式 7种实现方式
  • java小心机(3)| 浅析finalize()
  • Java知识点总结(JDBC-连接步骤及CRUD)
  • JS专题之继承
  • Linux gpio口使用方法
  • log4j2输出到kafka
  • PyCharm搭建GO开发环境(GO语言学习第1课)
  • Python学习之路13-记分
  • vue和cordova项目整合打包,并实现vue调用android的相机的demo
  • 动态规划入门(以爬楼梯为例)
  • 关于Java中分层中遇到的一些问题
  • 技术攻略】php设计模式(一):简介及创建型模式
  • 入职第二天:使用koa搭建node server是种怎样的体验
  • 我的面试准备过程--容器(更新中)
  • 详解NodeJs流之一
  • 详解移动APP与web APP的区别
  • 原生Ajax
  • 走向全栈之MongoDB的使用
  • 通过调用文摘列表API获取文摘
  • ​Z时代时尚SUV新宠:起亚赛图斯值不值得年轻人买?
  • ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
  • #传输# #传输数据判断#
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (Repost) Getting Genode with TrustZone on the i.MX
  • (板子)A* astar算法,AcWing第k短路+八数码 带注释
  • (多级缓存)多级缓存
  • (二十一)devops持续集成开发——使用jenkins的Docker Pipeline插件完成docker项目的pipeline流水线发布
  • (附源码)python旅游推荐系统 毕业设计 250623
  • (附源码)ssm考生评分系统 毕业设计 071114
  • (力扣题库)跳跃游戏II(c++)
  • (推荐)叮当——中文语音对话机器人
  • (转)Sql Server 保留几位小数的两种做法
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • * CIL library *(* CIL module *) : error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodu
  • .NET 常见的偏门问题