window.open 和 iframe 打开子窗口 使用 postMessage 实现父子窗口传值示例

一、语法

otherWindow.postMessage(message, targetOrigin);
  • otherWindow 其他窗口的一个引用,比如iframe的contentWindow属性、执行window.open返回的窗口对象、或者是命名过或数值索引的window.frames。(可以理解为目标窗口,即接收消息的窗口)
  • postMessage中第一个参数代表要传回的值,第二个是限定消息接收范围(域名设置),不限制使用 '*’

二、window.open打开子窗口的方式之跨窗口传递参数案例

(1) 父传子

父窗口 代码

<template>
	<div @click="onOpenChildWindow">打开子窗口</div>
	<el-button @click="onSendMsgToChild">点击发送消息给子窗口</el-button>
</template>
<script lang="ts" setup>
	//子窗口的window对象
	let childWindow = ref();
	//打开子窗口
	const onOpenChildWindow = () => {
		childWindow .value = window.open("/child","_blank")
	}
	//发送消息给指定子窗口
	const onSendMsgToChild = () => {
		childWindow.value.postMessage("我是父窗口传递的参数",*)
	}

</script>

子窗口 代码

<script lang="ts" setup>
	// 接收postMessage发送的消息
	window.addEventListener('message',function(e){
        /**
        * 此 if 作为来源检查。
        * 因为此监听可以接收任意来源的 postmessage
        * */ 
        if (window.location.origin !== e.origin) {
            return ;
        }
        console.log(e.data)//打印结果:我是父窗口传递的参数
    },false)
</script>

(2) 子传父

父窗口 代码

<template>
	<div @click="onOpenChildWindow">打开子窗口</div>
</template>
<script lang="ts" setup>
	//打开子窗口
	const onOpenChildWindow = () => {
		window.open("/child","_blank")
	}
	
	// 接收postMessage发送的消息
	window.addEventListener('message', function(e) {
		/**
		 * 此 if 作为来源检查。
		 * 因为此监听可以接收任意来源的 postmessage
		 * */
		if (window.location.origin !== e.origin) {
			return ;
		}
		console.log(e.data);//打印结果:我是子窗口传递的参数
	}, false);
</script>

子窗口 代码

<template>
	<el-button @click="onSendMsgToParent">点击发送消息给父窗口</el-button>
</template>
<script lang="ts" setup>
	const onSendMsgToParent = () => {
		window.opener.postMessage('我是子窗口传递的参数','*')
	}
</script>

三、iframe打开子窗口的方式之跨窗口传递参数案例

(1) 父传子

父窗口 代码

<template>
	<iframe src="/child" id="childIframe" width="500px" height="500px"></iframe>
	<el-button @click="onSendMsgToChild">向子页面发送消息</el-button>
</template>
<script lang="ts" setup>
	const onSendMsgToChild = () => {
		const childIframe = document.getElementById('childIframe');
		childIframe.contentWindow.postMessage('我是父窗口传递的参数','*');
	}
</script>

子窗口 代码

<script lang="ts" setup>
	// 接收postMessage发送的消息
	window.addEventListener('message',function(e){
        /**
        * 此 if 作为来源检查。
        * 因为此监听可以接收任意来源的 postmessage
        * */ 
        if (window.location.origin !== e.origin) {
            return ;
        }
        console.log(e.data)//打印结果:我是父窗口传递的参数
    },false)
</script>

(2) 子传父

父窗口 代码

<template>
	<div>我是父窗口</div>
	<iframe src="/y9vue-im/setting" id="childIframe" width="500px" height="500px"></iframe>
</template>
<script lang="ts" setup>
	// 接收postMessage发送的消息
	window.addEventListener('message', function(e) {
		/**
		 * 此 if 作为来源检查。
		 * 因为此监听可以接收任意来源的 postmessage
		 * */
		if (window.location.origin !== e.origin) {
			return ;
		}
		console.log(e.data);//打印结果:我是子窗口传递的参数
	}, false);
</script>

子窗口 代码

<template>
	<el-button @click="onSendMsgToParent">点击发送消息给父窗口</el-button>
</template>
<script lang="ts" setup>
	const onSendMsgToParent = () => {
		window.parent.postMessage('我是子窗口传递的参数','*')
	}
</script>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用`window.open`打开不同源的新窗口时,由于跨域安全策略的限制,无法直接在新窗口中设置sessionStorage和localStorage。这是因为浏览器对不同源的窗口之间的数据访问进行了限制。 但是,你可以通过以下方法在不同源的新窗口中传递数据: 1. 使用URL参数:将需要传递的数据作为URL参数附加到打开的URL中,然后在新窗口中解析URL参数来获取数据。例如: ```javascript var newWindow = window.open('https://example.com?data=value'); ``` 在新窗口的页面中,你可以使用JavaScript解析URL参数来获取数据。 2. 使用postMessage API:使用postMessage API可以实现窗口间的安全跨域通信。在打开的新窗口中,你可以使用`window.postMessage`发送消息,并在新窗口的页面中添加事件监听器来接收消息。例如: 在当前窗口: ```javascript var newWindow = window.open('https://example.com'); newWindow.postMessage('value', 'https://example.com'); ``` 在新窗口的页面中添加事件监听器: ```javascript window.addEventListener('message', function(event) { // 判断消息来源是否可信 if (event.origin === 'https://example.com') { // 获取传递的数据 var data = event.data; // 在新窗口中处理数据 sessionStorage.setItem('key', data); } }); ``` 通过以上方法,你可以在不同源的新窗口中传递数据,并在新窗口中进行处理。 请注意,使用postMessage API时需要确保消息来源可信,以防止恶意代码的注入和跨站点脚本攻击。 希望这可以帮助到你!如果你有更多问题,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值