当前位置:网站首页>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()
},
边栏推荐
- Arduino stepper library drive 28byj-48 stepper motor test program
- How can a programmer grow rapidly
- How can I know if I want to get the preferential link of stock account opening? Is it safe to open an account online?
- ORA-01950
- Technology T3 domestic platform! Successfully equipped with "Yihui domestic real-time system sylixos"
- Hls4ml entry method
- windows环境 redis安装和启动(后台启动)
- List is divided into sets that meet and do not meet conditions (partitioningby)
- 【C语言】详解 memset() 函数用法
- PowerDesigner design name and comment replacement
猜你喜欢
随机推荐
Bind this of the current scope for callback functions in other cases such as timers and delayers
独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作
实例讲解将Graph Explorer搬上JupyterLab
Getting started with fastdfs
DS transunet: Dual Swing transformer u-net for medical image segmentation
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
[research materials] national second-hand housing market monthly report January 2022 - Download attached
How can I know if I want to get the preferential link of stock account opening? Is it safe to open an account online?
Sum the amount
MYSLQ十种锁,一篇文章带你全解析
Hls4ml reports an error the board_ part definition was not found for tul. com. tw:pynq-z2:part0:1.0.
Hls4ml/vivado HLS error reporting solution
Source code series of authentic children -inheritablethreadlocal (line by line source code takes you to analyze the author's ideas)
实战项目笔记(一)——虚拟机的创建
STC 32-bit 8051 single chip microcomputer development example tutorial II i/o working mode and its configuration
300题线性代数 第四讲 线性方程组
寫博客文檔
PHP获取微信小程序和小程序商店外链地址
What did you learn about cheating after you went to college?
Use of common built-in classes of JS









