跨iframe 执行js函数

Posted on 2010-07-30 19:19 千羽天明

主窗口中的代码:

1
2
3
4
5
6
< script type = "text/javascript" >
    window.gogo = function (){alert('这是来自主窗口的信息');}
</ script >
< iframe id = "test" name = "test" src = "testb.html" width = "200" height = "300" style = "border:3px solid #333;" ></ iframe >
< input type = "button" value = "执行iframe中的函数"  onclick = "window.frames['test'].gogo()" />

Iframe中的代码:

1
2
3
4
< script type = "text/javascript" >
    window.gogo = function(){alert('这是来自iframe的信息');}
</ script >
< input type = "button" value = "执行主窗口中的函数" onclick = "window.parent.gogo()" />

window.frames['test'].gogo() 也可以写成 window.frames['test'].window.gogo(),但window.frames['test'] != window.frames['test'].window

alert( window.frames['test'] ) 返回的是 object DOMWindow, alert(window.frames['test'].window) 同样返回的是object DOMWindow,看来这里也能默认window。

同理:

window.frames['test'].window == document.getElementById('test').contentWindow

window.frames['test'] == document.getElementById('test').contentWindow

在ie6 ie7 ie8 firefox3.6 chrome中测试通过

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值