html_ifream与父级窗口之间的数据传递(window和document)
  6DMaaPzJglxt 2023年12月05日 23 0



ifream的window和document使用

  • ifream到上一层窗口
  • 代码部分
  • 方法:使用window.parent.location.href
  • 传递数据(ifreame到父级窗口)
  • contentWindow的使用
  • 跨域传递参数


ifream到上一层窗口

思路:
1.修改顶层窗口的href为ireame的url
2.修改顶层窗口的document为ifream的document

代码部分

方法:使用window.parent.location.href

修改父级的href
window.parent.location.href

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>测试</title>
	</head>
	<style>
		*{
			margin:0;
			padding:0;
		}
		body{
			width:100vw;
			height: 100vh;
		}
	</style>
	<body>
		<p>ifream窗口</p>
		<button onclick="test()">ifream跳出</button>
	</body>
	<script type="text/javascript">
		function test(){
			window.parent.location.href='http://127.0.0.1:8848/temp_file/child.html'
		}
	</script>
</html>

调用ifream的网页

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>home</title>
	</head>
	<style>
		*{
			margin:0;
			padding:0;
		}
		body{
			width:100vw;
			height: 100vh;
		}
		.child{
			position: absolute;
			width: 100%;
			height: 100%;
		}
	</style>
	<body>
		<p>父级窗口</p>
				<div class="box">
			<iframe src="child.html" class="child" id='iframe1'></iframe>
		</div>
	</body>
</html>

html_ifream与父级窗口之间的数据传递(window和document)_javascript

传递数据(ifreame到父级窗口)

contentWindow的使用

思路:传递window对象
ifream的html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>测试</title>
	</head>
	<style>
		*{
			margin:0;
			padding:0;
		}
		body{
			width:100vw;
			height: 100vh;
		}
	</style>
	<body>
		<p>ifream窗口</p>
		<button onclick="test()">ifreaamwindow</button>
		<p>发送的数据:windwo.postMapdata={name:'yma16'}<p>
	</body>
	<script type="text/javascript">
		function test(){
			window.postData={name:'yma16',pwd:'life'}
		}
	</script>
</html>

调用ifream的html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>home</title>
	</head>
	<style>
		*{
			margin:0;
			padding:0;
		}
		body{
			width:100vw;
			height: 100vh;
		}
		.child{
			position: absolute;
			width: 100%;
			height: 100%;
		}
	</style>
	<body>
		<p>父级窗口</p>
		<button onclick="getchild()">获取ifream的window</button>
		<hr>
		<p>父级数据显示</p>
		<input type="text" value='' id="parent_input" />
		<hr>
		<div class="box">
			<iframe src="child.html" class="child" id='iframe1'></iframe>
		</div>
	</body>
		<script type="text/javascript">
			function getchild(){
				document.getElementById('parent_input').value=JSON.stringify(document.getElementById('iframe1').contentWindow.postData)
				console.log("获取ifream的windows和documnet",document.getElementById('iframe1'));
				console.log('ifream的window',document.getElementById('iframe1').contentWindow)
				console.log('ifream的window的postMapData',document.getElementById('iframe1').contentWindow.postData)
				console.log('ifream的document',document.getElementById('iframe1').contentDocument)
			}
		</script>
</html>

html_ifream与父级窗口之间的数据传递(window和document)_html_02

跨域传递参数

ifreame界面,使用window.parent.postMessage(data,‘ip+port’)

window.parent.postMessage(data,'*')

父级窗口添加监听事件获取数据

window.addEventListener('message',this.listenIfreame())
listenIfreame:function(e){
console.log('message',e,e.data);
}


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月05日 0

暂无评论

推荐阅读
  wURKzOHw9Irf   2023年12月24日   30   0   0 HTMLicoicohtml
  8l4CZpTOKa7P   2023年12月26日   40   0   0 htmlhtml
  dwHry2iKGG0I   2023年12月26日   31   0   0 githubgithubhtmlhtml
6DMaaPzJglxt