使用Javascript访问父级中的iframe变量 [英] Accessing Iframe Variable in Parent With Javascript

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

问题描述

我知道在Stack Overflow上已经有令人难以置信的类似问题,但是我尝试了其中的许多问题,却一无所获.我正在尝试从子iframe抓取一个变量,以便在父窗口中使用.

I'm aware there are incredibly similar questions on Stack Overflow already for this, but I've tried MANY of them, and am just getting nothing. I'm trying to grab a variable from the child iframe to use in the parent window.

在child.html头标记中

In child.html head tag

<script type="text/javascript">
    var myVar="1";
</script>

在parent.html

In parent.html

<script type="text/javascript">
function load()
{
    var scroll="0"; 
    scroll = window.myIframe.myVar;
    if (scroll == "0") DO SOMETHING;
    else DO SOMETHING ELSE;
}
</script>
<iframe src="child.html" name="myIframe" onload="load()">
<p>Your browser does not support iframes.</p>
</iframe>

无论我如何尝试,都无法滚动以从子iframe抓取myVar变量.这几乎是Stack Overflow和其他论坛上人们常说完美的例子.任何想法我在做什么错了吗?

And no matter what I try, I cannot get scroll to grab the myVar variable from the child iframe. This is nearly verbatim of examples on Stack Overflow and other forums that people say work perfectly; any ideas what I'm doing wrong?

编辑:它们在同一个域中.

They are on the same domain.

推荐答案

在iframe中加载页面时,尝试从子内部访问oad().

Try to access oad() from inside child when the page loads in iframe.

添加子项:

<body onload="parent.load()">

此外,您可以更改要传递的代码,并在load(prm)中获取该变量作为参数.

Also, you can change the code to pass and get the variable as parameter in load(prm) .

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

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