最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vuejs recieve event from iframe using postmessage - Stack Overflow

programmeradmin4浏览0评论

according to this topic Calling a parent window function from an iframe

Say that iframe can send data back to parent window.

Now I implement it using vuejs and my code is

<v-card>
   <iframe :src="url" style="width:100%; height:100vh; border:0px;"></iframe>
</v-card>

and in iframe url I put the script like

$('#uploadbtn').bind("click",function(){
     window.parent.postMessage({
       'func': 'parentFunc',
        'message': 'Message text from iframe.'
     }, "*");
});

when user click button it will send message back to parent (vue ponent).

My question is how can I write code in VueJS side to get message from iframe?

Thank you

according to this topic Calling a parent window function from an iframe

Say that iframe can send data back to parent window.

Now I implement it using vuejs and my code is

<v-card>
   <iframe :src="url" style="width:100%; height:100vh; border:0px;"></iframe>
</v-card>

and in iframe url I put the script like

$('#uploadbtn').bind("click",function(){
     window.parent.postMessage({
       'func': 'parentFunc',
        'message': 'Message text from iframe.'
     }, "*");
});

when user click button it will send message back to parent (vue ponent).

My question is how can I write code in VueJS side to get message from iframe?

Thank you

Share Improve this question asked Jul 2, 2020 at 5:14 GiffaryGiffary 3,12813 gold badges53 silver badges76 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 15

You would need to attach an event listener on the window for message events.

For example (in your ponent)

methods: {
  receiveMessage (event) {
    console.log(event.data)
  }
},
mounted () {
  window.addEventListener('message', this.receiveMessage)
},
beforeDestroy () {
  window.removeEventListener('message', this.receiveMessage)
}

See https://developer.mozilla/en-US/docs/Web/API/Window/postMessage#The_dispatched_event

发布评论

评论列表(0)

  1. 暂无评论