iframe 实现跨域,两页面之间的通信

一、 背景

一个项目为vue2,一个项目为vue3,两个不同的项目实现iframe嵌入,并实现通信

二、方案

iframe跨域时,iframe组件之间常用的通信,主要是H5的possmessage方法

三、案例代码

父页面-vue2(端口号为127.0.0.1:8080)

<template>
  <div>
    <div class="container">
      <iframe
        ref="iframeId"
        id="iframeId"
        src="http://127.0.0.1:8081"
        frameborder="0"
        border="0"
        hspace="0"
        vspace="0"
        scrolling="yes"
        height="100%"
        width="100%"
      ></iframe>
    </div>
  </div>
</template>
<script lang="ts">
import { Vue, Component } from 'vue-property-decorator';

@Component({
  components: {},
})
export default class FeatureService extends Vue {
  // 定义一个方法,用于发送消息到iframe
  private postMessageToIframe() {
    // 设置延时,确保iframe已经加载完成
    setTimeout(() => {
      // 定义要发送的数据
      const iframeInfo = { isIframeParent: true };
      // 定义目标源
      const targetOrigin = 'http://127.0.0.1:8081';
      // 获取iframe引用
      const iframe = this.$refs.iframeId;
      // 向iframe发送消息
      iframe.contentWindow.postMessage(
        JSON.stringify(iframeInfo),
        targetOrigin
      );
    }, 500);
  }

  private mounted() {
    // 在组件挂载后,发送消息到iframe
    this.postMessageToIframe();

    window.addEventListener('message', this.handleMessage, false);
  }

  private handleMessage(event: MessageEvent) {
    // 通过origin对消息进行过滤,避免遭到XSS攻击
    if (event.origin === 'http://127.0.0.1:8081') {
      console.log('子页面传输过来参数', event.data);
    }
  }
}
</script>
<style lang="less" scoped>
.container {
  width: 100%;
  margin-top: -40px;
  height: 100vh;
  overflow: hidden;
  #iframeId {
    width: 100%;
    display: block;
  }
}
</style>

子页面-vue3, (端口号为127.0.0.1:8081)

<template>
  <div>
    <!-- 顶部导航 -->
    <TopMenu v-if="!iframeParentInfo.isIframeParent" />

    <div class="demo-sidebar-container demo-full-width">
      <!-- 侧边栏 -->
      <SiderBar v-if="!iframeParentInfo.isIframeParent" />
      <!-- 内容容器 -->
      <ContentBox />
    </div>
  </div>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'

const iframeParentInfo = ref({})

const messageHandler = (e) => {
  // 通过origin对消息进行过滤,避免遭到XSS攻击
  if (e.origin !== 'http://127.0.0.1:8080') return
  if (typeof e.data === 'string') {
    parseData(e.data)
  }
}

const parseData = (data) => {
  try {
    iframeParentInfo.value = JSON.parse(data)
    console.log('父页面传输过来参数', data)
  } catch (error) {
    console.error('解析JSON出错', error)
    iframeParentInfo.value = {}
  }
}

onMounted(() => {
  // 获取 父向 子(iframe) 传递的信息
  window.addEventListener('message', messageHandler)

  // 子(iframe)向父传递信息
  window.parent.postMessage('Hello Parent!', '*')
})
</script>

四、案例效果

在这里插入图片描述

五、补充

ps: 以上案例可能会发现 父窗口能拿到子iframe的信息,而子iframe拿不到父窗口的信息

解决方案

this.$nextTick
在 Vue 完成下一次 DOM 更新后,添加一个事件监听器,用于监听 message 事件,并在事件触发时调用 this.handleMessage 方法处理事件。

案例优化

父窗口

<template>
  <div>
    <div class="container">
      <iframe
        ref="iframeId"
        id="iframeId"
        src="http://127.0.0.1:8081"
        frameborder="0"
        border="0"
        hspace="0"
        vspace="0"
        scrolling="yes"
        height="100%"
        width="100%"
      ></iframe>
    </div>
  </div>
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';

@Component({
  components: {},
})
export default class FeatureService extends Vue {
  // 定义一个方法,用于发送消息到iframe
  private postMessageToIframe() {
    // 定义要发送的数据
    const iframeInfo = { isIframeParent: true };
    // 定义目标源,对应iframe子窗口的地址
    const targetOrigin = 'http://127.0.0.1:8081';
    // 获取iframe引用
    const iframe = this.$refs.iframeId as HTMLIFrameElement;
    // 向iframe发送消息
    iframe?.contentWindow?.postMessage(
      JSON.stringify(iframeInfo),
      targetOrigin,
    );
  }

  private created() {
    // 在组件挂载后,发送消息到iframe
    this.$nextTick(() => {
      window.addEventListener('message', this.handleMessage, false);
    });
  }

  private handleMessage(event: MessageEvent) {
    // 通过origin对消息进行过滤,避免遭到XSS攻击,对应iframe子窗口的地址
    if (event.origin === 'http://127.0.0.1:8081') {
      console.log('子页面传输过来参数', event.data);
      this.postMessageToIframe();
    }
  }
  private unmounted() {
    window.removeEventListener('message', (e) => this.handleMessage(e));
  }
}
</script>
<style lang="less" scoped>
.container {
  width: 100%;
  margin-top: -40px;
  height: 100vh;
  overflow: hidden;
  #iframeId {
    width: 100%;
    display: block;
  }
}
</style>

子窗口

<template>
  <div>
    <!-- 顶部导航 -->
    <TopMenu v-if="!iframeParentInfo.isIframeParent" />

    <div class="demo-sidebar-container demo-full-width">
      {{ iframeParentInfo.isIframeParent }}
      <!-- 侧边栏 -->
      <SiderBar v-if="!iframeParentInfo.isIframeParent" />
      <!-- 内容容器 -->
      <ContentBox />
    </div>
  </div>
</template>
<script setup lang="ts">
import ContentBox from '@/common/components/ContentBox.vue'
import SiderBar from '@/common/components/SiderBar.vue'
import TopMenu from '@/common/components/TopMenu.vue'
import { useFeatureStore, useKVBankStore } from '@/stores'
import { onMounted, onUnmounted, ref } from 'vue'
import useRentManageStore from './views/authorityManage/rentManage/store'

// isIframeParent为false
const iframeParentInfo = ref({ isIframeParent: false })

// 存储信息
const featureStore = useFeatureStore()


const messageHandler = (e: any) => {
  console.log('子=====e', e)
  // 通过origin对消息进行过滤,避免遭到XSS攻击 e.origin对应父窗口的地址
  if (e.origin !== 'http://127.0.0.1:8080') return
  if (typeof e.data === 'string') {
    parseData(e.data)
  }
}

const parseData = (data: any) => {
  console.log('---4222--', data)
  try {
    iframeParentInfo.value = JSON.parse(data)
    console.log('父页面传输过来参数', data)
  } catch (error) {
    console.error('解析JSON出错', error)
    iframeParentInfo.value = { isIframeParent: false }
  }
  featureStore.getFeatureInfo(iframeParentInfo.value)
}



onMounted(() => {
  // 获取 父向 子(iframe) 传递的信息
  window.addEventListener('message', messageHandler, false)
  console.log('子==window.location.location', window.location)
  // 子(iframe)向父传递信息
  window.parent.postMessage('Hello Parent!', '*')
})
onUnmounted(() => {
  window.removeEventListener('message', messageHandler)
})
</script>

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现iframe跨域访问页面,需要满足以下条件: 1. 父页面和子页面都是自己可以控制的。 2. 被嵌入的页面不属于同一个域名、协议或端口号的源。 实现iframe跨域访问页面可以采用以下方法: 1. 使用postMessage:通过在父页面和子页面之间进行消息传递,可以安全地实现跨域通信。父页面可以使用`window.postMessage()`方法向子页面发送消息,子页面可以通过监听`message`事件接收并处理这些消息。这种方式允许双向通信,并且在不同的窗口间传递数据时更加安全可靠。 2. 设置document.domain:如果父页面和子页面具有相同的顶级域名,但不同的子域名,可以通过设置相同的document.domain来实现跨域访问。例如,如果父页面的域名是`example.com`,子页面的域名是`sub.example.com`,可以在父页面和子页面中都设置`document.domain = "example.com"`。这样,它们就可以进行跨域通信。 3. 使用CORS(跨域资源共享):在服务器端配置响应文件的HTTP头,允许来自其他域的页面访问。服务器需要设置特定的响应头,包括`Access-Control-Allow-Origin`来指定允许访问的域名。 总结起来,要实现iframe跨域访问页面,可以使用postMessage、设置document.domain或使用CORS来进行跨域通信。这些方法都可以确保安全地进行跨域访问,并实现在父页面和子页面之间的数据传递和通信
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值