当前位置:网站首页>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()
},
边栏推荐
- math_ Use differentiation to calculate approximate value
- docker ubuntu容器中安装mysql遇到的问题
- Arduino stepper library drive 28byj-48 stepper motor test program
- Tensorflow reports an error, could not load dynamic library 'libcudnn so. eight
- What did you learn about cheating after you went to college?
- 走进如心小镇,数智化变革连接“未来社区”
- Oracle 死锁测试
- Set object value changes null value object
- [Blue Bridge Cup web] analysis of the real topic of the 13th Blue Bridge Cup web university group match in 2022
- SwiftUI 4 新功能大全之 Toggle与 Mixed Toggle 多个绑定组件
猜你喜欢

Simple but modern server dashboard dashdot

漏洞复现-.Net-ueditor上传

Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?

Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation

Hls4ml reports an error the board_ part definition was not found for tul. com. tw:pynq-z2:part0:1.0.

Interview question 1

math_ Use differentiation to calculate approximate value

Detailed configuration of network security "Splunk" in national vocational college skills competition

振弦采集模塊測量振弦傳感器的流程步驟

After adding cocoapods successfully, the header file cannot be imported or an error is reported in not found file
随机推荐
SQL getting started plan-1-select
Bind this of the current scope for callback functions in other cases such as timers and delayers
Hls4ml/vivado HLS error reporting solution
[Mysql]安装Mysql5.7
How to prevent repeated submission of new orders
Flask 常用组件
Develop those things: easycvr platform adds playback address authentication function
What if win11 can't pause the update? Win11 pause update is gray. How to solve it?
STC 32位8051单片机开发实例教程 三 程序编译设置与下载
[research data] observation on the differences of health preservation concepts among people in 2022 - Download attached
2022/6/8-2022/6/12
Optimization of the problem that the request flow fails to terminate during page switching of easycvr cluster video Plaza
300题线性代数 第四讲 线性方程组
Stack overflow 2022 developer survey: where is the industry going?
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
Technology T3 domestic platform! Successfully equipped with "Yihui domestic real-time system sylixos"
Win11怎么关闭开机自启动软件
Use Zadig to build a continuous delivery platform from 0 to 1