当前位置:网站首页>Iframe parent-child page communication
Iframe parent-child page communication
2022-07-01 20:05:00 【Laocheng】
1、 Father sends message to son , Receive messages from sub
Parent page related code
<div class="iframeWrap">
<iframe ref="udaamIframe" class="udaamIframe" style="width:100%;height:100%" :src="linkUrl" frameborder="0" />
</div>
data() {
return {
linkUrl: this.$route.meta.linkUrl
}
},
// vue Mount stage
mounted() {
this.iframeTalk()
},
methods: {
iframeTalk() {
// You have this parameter needSendToken Need to communicate
if (this.linkUrl.includes('needSendToken=true')) {
const iframe = this.$refs.udaamIframe // This is vue The grammar of , You can replace with the following line
// document.querySelector('.iframeWrap .udaamIframe')
var fatherApp = iframe.contentWindow
// Dealing with compatibility issues
if (iframe.attachEvent) {
// When iframe Send a message after loading , Otherwise iframe Don't get
iframe.attachEvent('onload', () => {
this.sendToken(fatherApp)
})
} else {
iframe.onload = () => {
this.sendToken(fatherApp)
}
}
window.udemrFirstLoginOut = true // Avoid triggering multiple times
// Receive sub application messages
window.addEventListener('message', e => {
// <!-- To the source origin Do some filtering , Avoid receiving messages of illegal domain names xss attack -->
if (e.data && e.data.origin === 'child' && e.data.isLoginOut === true && window.udemrFirstLoginOut) {
console.log(' received child Message sent You need to help them log out , e:', e)
window.udemrFirstLoginOut = false
store.dispatch('LogOut')
}
}, false)
}
},
sendToken(udaamApp) {
console.log(' Father Send a message to the external link Application , The message is token')
udaamApp.postMessage(
{
type: 'sendToken',
token: localStorage.getItem('token'),
remark: ' ABA, ABA, ABA '
},
'*'
)
}
}
2、 The child receives messages from the parent 、 Send a message to my father
Sub page related code
// Receive messages from parents
window.addEventListener('message', function(e) {
if (e.data && e.data.type === 'sendToken') {
console.log(' Received a message from the main application ', e)
localStorage.setItem('token', e.data.token)
}
})
// The son sends a message to the father
const params = {
origin: 'child',
isLoginOut: true
}
window.parent.postMessage(params, '*')
Add
because load iframe It's asynchronous 、 iframe The message received from the main application is also asynchronous 、iframe Loading the corresponding module according to the route is also asynchronous 、 When the corresponding module is loaded api Requests are also asynchronous ;
So many asynchronies , What has been avoided is that the main application must send messages in iframe Messages sent after receiving messages after loading , That is, sub application iframe You must receive messages from the main application normally ;
But sub applications iframe Received a message and Its page tone api Requests are uncontrollable , And we need iframe Send the request after receiving the message ( You need to bring it from your father token), So we need to deal with :
Ideas as follows : Find the module that needs to send the request , Find out which request it needs to send first , Poll the message waiting to receive the main application before sending the request , The polling is not finished until the message is received. It is initiated normally api Request to load the page ;
// Wait until you receive the message from the main application Proceed again api request ( Because the request needs to carry the data transmitted by the main application token)
timeout: null
setWaitToken() {
const getToken = () => {
this.timeout && clearTimeout(this.timeout)
this.timeout = setTimeout(() => {
if (localStorage.token) {
this.yourFirstApi() // This page needs to be initiated first api request
} else {
getToken()
}
}, 100)
}
getToken()
},
边栏推荐
- Mo Tianlun salon | Tsinghua qiaojialin: Apache iotdb, originated from Tsinghua, builds an open source ecological road
- Interview questions shared in today's group
- 【let var const】
- 有意思了!数据库也搞Serverless!
- 数据分析师听起来很高大上?了解这几点你再决定是否转型
- Flask 常用组件
- Richview RichEdit srichviewedit PageSize page setup and synchronization
- Keras machine translation practice
- How can I know if I want to get the preferential link of stock account opening? Is it safe to open an account online?
- 独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作
猜你喜欢
Richview trvdocparameters page parameter settings
RichView 文档中的 ITEM
墨天轮沙龙 | 清华乔嘉林:Apache IoTDB,源于清华,建设开源生态之路
为定时器和延时器等其它情况的回调函数绑定当前作用域的this
[research materials] national second-hand housing market monthly report January 2022 - Download attached
STC 32位8051单片机开发实例教程 二 I/O工作模式及其配置
牛客编程题--必刷101之字符串(高效刷题,举一反三)
简单但现代的服务器仪表板Dashdot
走进如心小镇,数智化变革连接“未来社区”
振弦采集模块测量振弦传感器的流程步骤
随机推荐
Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
300题线性代数 第四讲 线性方程组
On the next generation entrance of the metauniverse -- the implementation of brain computer interface
math_利用微分算近似值
Stack overflow 2022 developer survey: where is the industry going?
[untitled]
想得到股票开户的优惠链接,如何得知?在线开户是安全么?
Stack Overflow 2022 开发者调查:行业走向何方?
Entering Ruxin Town, digital intelligence transformation connects "future community"
EasyCVR集群视频广场页面切换时,请求流未能终止的问题优化
[C language] explain the usage of memset() function in detail
Redis installation and startup in Windows environment (background startup)
What if win11 can't pause the update? Win11 pause update is gray. How to solve it?
Servlet knowledge points
写博客文档
What did you learn about cheating after you went to college?
Win11怎么关闭开机自启动软件
Simple but modern server dashboard dashdot
Understand the structure in C language in one article
Win11如何取消任务栏隐藏?Win11取消任务栏隐藏的方法