从父级访问iframe的变量 [英] Access a variable of iframe from parent

查看:88
本文介绍了从父级访问iframe的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iframe的脚本

 < script type =text / javascript> 
var a = 5;
< / script>

父窗口的脚本

 < script type =text / javascript> 
function close()
{
var check = document.getElementById(iframeid)。contentDocument.a;
alert(check)
}
< / script>

我想访问从父级iframe中定义的变量。

解决方案

使用 contentWindow 而不是 contentDocument 适用于我:

  var check = document.getElementById(iframeid)。contentWindow.a; 

此外,确保域匹配,并且您正在使用Web服务器进行测试警告从文件系统测试)。


script of iframe

<script type="text/javascript" >
var a=5;
</script>

script of parent window

<script type="text/javascript" >
function close()
{
var check=document.getElementById("iframeid").contentDocument.a;
alert(check)
}
</script>

I want to access the variable which is defined inside the iframe from parent. But the above code doesn't work properly can anyone give an idea to implement this.

解决方案

Using contentWindow instead of contentDocument works for me:

var check = document.getElementById("iframeid").contentWindow.a;

Also, ensure that the domains match and that you are using a webserver to test (I got a protocol warning when testing from the file system).

这篇关于从父级访问iframe的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆