vue页面中嵌入iframe外源地址页面 监听iframe 中事件

本文介绍在Vue项目中如何嵌入第三方URL的iframe,并监听其内部事件。当iframe内的页面触发特定事件时,Vue组件能够捕获到这一事件并执行关闭当前页面的操作。涉及到的技术点包括Vue.js和HTML的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.需求 vue中需要嵌入iframe 地址是第三方的 当iframeh中页面点击的时候.vue需要监控到这个事件将本页面关闭.代码中 js 发送和接收方法

//vue组件中是个弹框
<template>
 <el-dialog title="编辑" :visible.sync="editDialog" center :fullscreen="true" append-to-body v-loading="loading">
      <iframe :src="iframeurl" frameborder="0" style="width:100%;" :height="height"></iframe>
 </el-dialog>
</template>

<script>
export default{
data(){
   return{
     iframeurl:"******"  //第三方页面地址
        }
     },
   mounted:{
     //监听iframe变化
        window.addEventListener('message', (eve) => {
        console.log(eve)
       })

   }

 }
</script>


//第三方页面 比如纯html  页面
<!DOCTYPE HTML>
<html>
<head>
    <title>Demo</title>
    <meta charset="utf-8">
    <style>
     
    </style>
</head>
<body>
### Vue嵌入 iframe 并调用其内部事件Vue 项目中实现与 `<iframe>` 的交互主要通过 `postMessage` API 来完成跨域安全的消息传递。这种方式允许父页面向子页面发送消息以及接收来自子页面的通知。 #### 创建带有 iframe 的组件结构 定义一个简单的 Vue 组件来加载目标网站: ```vue <template> <div class="container"> <!-- 定义 iframe --> <iframe ref="myIframe" :src="url" @load="onLoad"></iframe> <!-- 操作按钮 --> <button @click="sendMessageToIFrame">Send Message to iFrame</button> </div> </template> <script> export default { data() { return { url: &#39;https://example.com&#39;, // 替换成实际 URL }; }, methods: { onLoad(event) { console.log(&#39;iFrame loaded&#39;); window.addEventListener(&#39;message&#39;, this.receiveMessage); }, sendMessageToIFrame() { const message = { action: "doSomething", payload: {} }; // 获取 iframe DOM 对象并通过 contentWindow 发送消息给子页面 this.$refs.myIframe.contentWindow.postMessage(message, &#39;*&#39;); }, receiveMessage(event) { try { if (event.origin !== &#39;https://example.com&#39;) throw new Error(`Invalid origin ${event.origin}`); switch(event.data.action){ case &#39;response&#39;: alert(JSON.stringify(event.data.payload)); break; default: console.warn(&#39;Unknown event received from child frame.&#39;); } } catch(error) { console.error(error.message); } } }, beforeDestroy(){ window.removeEventListener(&#39;message&#39;, this.receiveMessage); } } </script> ``` 上述代码展示了如何创建一个包含 `<iframe>` 的 Vue 组件,并设置了一个点击按钮用来触发向 iframe 内部发送消息的操作。当 iframe 加载完成后,监听器被注册以便能够接收到由 iframe 返回的信息[^2]。 为了使这段逻辑正常工作,在 iframe 页面也需要相应的处理程序来响应这些请求并将结果反馈回去。假设我们控制着 iframe 所指向的内容,则可以在该页面上添加如下 JavaScript 代码片段: ```javascript window.onmessage = function(e) { var origin = e.origin || e.originalEvent.origin; if(origin === &#39;https://parent-domain.com&#39;){ // 验证来源域名 let responsePayload = {}; switch(e.data.action){ case &#39;doSomething&#39;:{ // 这里放置具体的业务逻辑 responsePayload = {"status": true}; break; } default:{ console.log(&#39;Unrecognized command:&#39;, e.data.action); } } parent.postMessage({action:&#39;response&#39;,payload:responsePayload},&#39;*&#39;); } }; ``` 这样就可以实现在 Vue 主页和 iframe页面之间的双向通讯机制了。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值