当前位置:网站首页>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()
},
边栏推荐
- 8K HDR!|为 Chromium 实现 HEVC 硬解 - 原理/实测指南
- 写博客文档
- DS transunet: Dual Swing transformer u-net for medical image segmentation
- The large list set is divided into multiple small list sets in equal proportion
- MYSLQ十种锁,一篇文章带你全解析
- 运动捕捉系统原理
- 渗透工具-TrustedSec 公司的渗透测试框架 (PTF)
- Develop those things: easycvr platform adds playback address authentication function
- 300题线性代数 第四讲 线性方程组
- 关于new Set( )还有哪些是你不知道的
猜你喜欢
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
Arduino stepper library drive 28byj-48 stepper motor test program
Technology T3 domestic platform! Successfully equipped with "Yihui domestic real-time system sylixos"
Myslq ten kinds of locks, an article will take you to fully analyze
牛客编程题--必刷101之字符串(高效刷题,举一反三)
Simple but modern server dashboard dashdot
为定时器和延时器等其它情况的回调函数绑定当前作用域的this
墨天轮沙龙 | 清华乔嘉林:Apache IoTDB,源于清华,建设开源生态之路
全国职业院校技能大赛网络安全“splunk“详细配置
由浅入深学会白盒测试用例设计
随机推荐
Bind this of the current scope for callback functions in other cases such as timers and delayers
Using qeventloop to realize synchronous waiting for the return of slot function
Win11快捷键切换输入法无反应怎么办?快捷键切换输入法没有反应
对金额进行求和
docker ubuntu容器中安装mysql遇到的问题
What did you learn about cheating after you went to college?
数据分析师听起来很高大上?了解这几点你再决定是否转型
墨天轮沙龙 | 清华乔嘉林:Apache IoTDB,源于清华,建设开源生态之路
Related concepts of cookies and sessions
math_ Use differentiation to calculate approximate value
3D全景模型展示可视化技术演示
internship:复杂json格式数据编写接口
How to use console Log print text?
Technology T3 domestic platform! Successfully equipped with "Yihui domestic real-time system sylixos"
C # joint halcon Application - Dahua Camera Collection class
大厂做狼,小厂做狗?
Set object value changes null value object
A quietly rising domestic software, low-key and powerful!
RichView TRVDocParameters 页面参数设置
Oracle 死锁测试