浏览器主页、iframe、子窗口的互相操作

主页操作iframe

var ifs = document.getElementById('iframeID').contentWindow.document;  \\获取到iframe的document对象
document.getElementById('iframeID').contentWindow.jsMethod(); \\调用iframe的JS方法
ifs.getElementById('elementID').onclick(function(){alert('function binding in main page!');});  \\在主页中为iframe绑定主页的方法

子iframe操作主页
var pp = parent.document; \\获取主页的document
parent.jsMethod(); \\调用主页JS方法
pp.getElementById('elementID').onclick(function(){alert('function binding in main page!');});  \\在iframe中为主页绑定iframe中的方法

主窗口操作子窗口
var nw = window.open("about:blank"); 
var nd = nw.document; \\获取子窗口的document
nw.jsMethod(); \\调用子窗口JS方法

子窗口中操作主窗口
var od = opener.document;  \\获取主窗口的document
opener.showJS();  \\调用主窗口的js

样例代码:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<script>
var nw;
var injectHtml = '<hr><div><p>inject success</p>\
					<div>iframe test<input type="button" value="test parent DOM" οnclick="parent.document.getElementById(\'showtext\').innerHTML=\'parent element changed!\'" >\
					<input type="button" value="test parent JS" οnclick="parent.showJS();" ></div>\
					<div>new window test<input type="button" value="test parent DOM" οnclick="opener.document.getElementById(\'showtext\').innerHTML=\'parent element changed!\'" >\
					<input type="button" value="test parent JS" οnclick="opener.showJS();" ></div>\
					<div>method binding<input type="button" value="open parent new window" id="bindingmethod" >\
					<input type="button" value="show iframe" id="binding2"></div>\</div>';
$(document).ready(function(){	
	
	
});
function injectWithIframe(html){
	if (nw) {nw.close()};
	var iframe = '<iframe src="about:blank" frameborder="0" name="FirebugUI" id="FirebugUI" style="border: 0px; visibility: visible; z-index: 2147483647; position: fixed; width: 100%; left: 0px; bottom: 0px; height: 241px; display: block;"></iframe>';
	$("#iframediv").append(iframe);
	var ifs = document.getElementById('FirebugUI').contentWindow.document;
	$(ifs.body).html(html);
	$('#bindingmethod', ifs.body).click(this.opennewwithhtml);
}
function opennewwithhtml(){
	openInNewWindow(injectHtml);
}
function openInNewWindow(html){
	$('#iframediv').empty();
	nw = window.open("about:blank");
	$(nw.document.body).html(html);	
	$('#binding2', nw.document.body).click(this.showIframe);
}
function showIframe(){
	injectWithIframe(injectHtml)
}
function showJS(){
	$('#showtext').text('parent JS called!');
}
</script>
</head>
<body>
<div>
<input type="button" value="inject iframe" οnclick="javascript:injectWithIframe(injectHtml)" >
<input type="button" value="inject new window" οnclick="javascript:openInNewWindow(injectHtml)" >
</div>
<div id="showtext"></div>
<div id="iframediv"></div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上帝De助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值