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

学校网站构建百度信息流代运营

学校网站构建,百度信息流代运营,冒险岛钓鱼网站做啥用,做自己视频教程的网站使用 postMessage API 来实现跨窗口(跨域)的消息传递。postMessage 允许你安全地发送消息到其他窗口,包括嵌套的 iframe,而不需要担心同源策略的问题。 发送消息(父应用) 1. 父应用:发送消息给…
使用 postMessage  API 来实现跨窗口(跨域)的消息传递。postMessage 允许你安全地发送消息到其他窗口,包括嵌套的 iframe,而不需要担心同源策略的问题。

发送消息(父应用)

1. 父应用:发送消息给子应用

父应用可以通过 postMessage 将消息发送给子应用。需要注意的是,发送消息时,需要指定目标窗口和目标窗口的来源(即目标 iframe 的 origin),以确保安全性。

 iframe.contentWindow.postMessage(message, targetOrigin)

 使用 postMessage 向子应用发送消息。message 是要发送的数据,可以是任何 JavaScript 对象。targetOrigin 是目标窗口的源(例如:http://child-app.com),它确保消息只发送到具有这个源的窗口。

2、接收消息(子应用)

子应用通过监听 message 事件来接收父应用发送的消息。收到消息后,子应用可以根据消息中的 type 字段来执行不同的操作。

示例:父应用与子应用通信

1. 父应用:发送消息给子应用

父应用通过 postMessage 向 iframe 中的子应用发送一个包含 type 的消息。我们还可以通过 targetOrigin 来确保消息只发送到指定的子应用。

<!-- 父应用 -->
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Parent App</title>
</head>
<body><h1>Parent Application</h1><!-- iframe 中嵌套子应用 --><iframe id="childIframe" src="http://child-app.com" width="600" height="400"></iframe><button onclick="sendMessageToChild()">发送消息到子应用</button><script>// 发送消息到子应用function sendMessageToChild() {const iframe = document.getElementById('childIframe');const message = {type: 'CLEAR_INPUT',  // 消息类型data: { message: '请清空输入框' }};// 使用 postMessage 发送消息到子应用iframe.contentWindow.postMessage(message, 'http://child-app.com');  // 子应用的 origin}</script>
</body>
</html>
2. 子应用:接收父应用发送的消息

子应用会监听 message 事件,接收到消息后可以处理它。通常可以根据 message.type 来决定如何处理消息。

<!-- 子应用 (iframe 内容) -->
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Child App</title>
</head>
<body><h1>Child Application</h1><input type="text" id="inputField" placeholder="请输入内容..."><script>// 监听父应用发送的消息window.addEventListener('message', function(event) {// 验证消息的来源if (event.origin !== 'http://parent-app.com') {return;  // 如果消息来源不正确,则忽略}const message = event.data;console.log('Received message from parent:', message);// 根据消息的 type 执行不同操作if (message.type === 'CLEAR_INPUT') {document.getElementById('inputField').value = '';  // 清空输入框}});</script>
</body>
</html>

如何在vue本地项目进行iframe测试通讯

1、新建一个iframe.vue页面,为父页面

// 父应用
<template><div class="act-form"><iframe :src="src" id="childIframe" width="600" height="400"></iframe><el-button @click="sendMessage">向iframe发送信息</el-button></div>
</template><script>export default {data () {return {src: '',}},created() {// 当前我本地运行的端口为 localhost:8080  需要根据实际情况调动this.src = 'http:localhost:8080/home?id=18' },methods: {sendMessage () {const iframe = document.getElementById('childIframe');const message = {type: 'CLEAR_INPUT',  // 消息类型data: { message: '请清空输入框' }};// 使用 postMessage 发送消息到子应用iframe.contentWindow.postMessage(message, this.src);  // 子应用的origin},},}
}
</script>

2、子应用 

<!-- 子应用 (iframe 内容) -->
<template><div> // 内容区域</div>
</template><script>export default {data () {return {},created() {// 在外部vue的window上添加postMessage的监听,并且绑定处理函数handleMessagewindow.addEventListener('message', this.handleMessage)},methods: {handleMessage (event) {// 根据上面制定的结构来解析iframe内部发回来的数据// 验证消息的来源if (event.origin !== 'http://parent-app.com') {return;  // 如果消息来源不正确,则忽略}const message = event.data;console.log('Received message from parent:', message);// 根据消息的 type 执行不同操作if (message.type === 'CLEAR_INPUT') {// 在这里执行你需要的逻辑}});}}beforeDestroy(){  // 页面关闭移除监听window.removeEventListener("message", this.handleMessage);}
</script>

http://www.ds6.com.cn/news/62019.html

相关文章:

  • 俄语网站开发百度指数关键词工具
  • 最便宜网站怎么给客户推广自己的产品
  • 商业摄影网站网络营销的主要内容包括
  • 搭建网页的基础语言seo搜论坛
  • 商丘做网站汉狮网络建立网站平台需要多少钱
  • 服装品牌网页设计图片seo标题优化分析范文
  • 企业网站的首页设计模板seo服务公司
  • 沈阳关键词优化报价seo咨询师招聘
  • wordpress调整配置百度seo公司电话
  • 濮阳家电网站建设网络营销有什么特点
  • 做直销哪个网站好网络营销员岗位的职责与要求
  • 深圳网站平台建设百度广告联盟收益
  • 新吴区推荐做网站电话哈尔滨优化调整人员流动管理
  • 网站后台怎么上传图片凡科网怎么建网站
  • 学网站设计和平面设计今日热点新闻事件摘抄
  • 那种web网站开发文档汽车营销活动策划方案
  • 兰州市科协网站外贸建站推广公司
  • b站推广网站2024不用下载微信怎么推广
  • 网站免费在线观看沈阳关键词快照优化
  • 最好的装饰公司营销型网站网站搜索引擎优化报告
  • 简单个人网站设计百度小说排行榜完本
  • 建设小说网站违法吗sem是什么设备
  • 怎么在工商网站做实名认证电商平台有哪些
  • asp网站建设mdb文件seo网站结构优化的方法
  • 长沙手机网站设计软件开发app制作
  • 织梦网站如何调用其他网站新闻个人开发app可以上架吗
  • 做同城网站最赚钱百度账号人工申诉
  • 站长如何做导航网站福州短视频seo推荐
  • 怎样建设淘客网站网站运营专员
  • 无锡做网站哪个公司好百度优化怎么做