从同一域上的父窗口访问 iframe 中的 JavaScript 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14366841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing JavaScript variable in an iframe, from the parent window on same domain
提问by Faldinio
I am fairly new to JavaScript and I am trying to get value of a variable from an embedded iframe. I have resorted to basic testing to make sure I am getting the method right...
我对 JavaScript 相当陌生,我正在尝试从嵌入式 iframe 中获取变量的值。我已经求助于基本的测试,以确保我得到了正确的方法......
I have tried the common suggestions and everything returns 'undefined'. I want to use the variable in an onclick event.
我已经尝试了常见的建议,一切都返回“未定义”。我想在 onclick 事件中使用该变量。
In the iframe content I have stripped everything and just for testing have this:
在 iframe 内容中,我删除了所有内容,仅用于测试:
<script>
var test = "test";
</script>
in the parent window I have done this:
在父窗口中,我已经这样做了:
<iframe src="iframe.html" id="iframe" width="200" height="100"></iframe>
<script>
var myVar = window.frames[0].window.test;
</script>
<a href="#" onclick='alert(myVar)' id="link">Click</a>
I have tried the options suggested here: grab global variable from an embedded iframe
我已经尝试了这里建议的选项:从嵌入式 iframe 中抓取全局变量
Everything I do returns undefined... am I doing something fundamentally wrong?
我所做的一切都返回未定义......我做错了什么吗?
Thanks in advance..
提前致谢..
回答by Bergi
You are already executing the script when the <iframe>is still loading, so it's testis undefined. Either wait for the iframe's loadevent, or just get the variable when clicking:
当<iframe>仍在加载时,您已经在执行脚本,因此它test是undefined. 要么等待 iframe 的load事件,要么在单击时获取变量:
<iframe src="iframe.html" id="iframe" width="200" height="100"></iframe>
<script>
var myFrame = window.frames[0].window;
</script>
<a href="#" onclick='alert(myFrame.test)' id="link">Click</a>
回答by Frédéric Hamidi
You're apparently not waiting for the frame's content to be loaded before accessing myVar. Chances are the <script>element in the frame has not yet been fetched or executed when you do that.
您显然没有在访问myVar. <script>当您执行此操作时,可能尚未获取或执行框架中的元素。
Try delaying the variable assignment until the frame's content is fully loaded:
尝试延迟变量赋值,直到框架的内容完全加载:
<iframe src="iframe.html" id="iframe" onload="frameLoaded();" width="200" height="100"></iframe>
<script>
var myVar;
function frameLoaded() {
myVar = window.frames[0].window.test;
}
</script>
As an aside, since your question is tagged jqueryI would suggest you write something like:
顺便说一句,由于您的问题被标记为jquery,我建议您编写如下内容:
<iframe src="iframe.html" id="iframe" width="200" height="100"></iframe>
<script>
$("#iframe").on("load", function() {
var myVar = this.window.test;
$("#link").on("click", function() {
alert(myVar);
});
});
</script>
回答by fkabeer
try to use this
尝试使用这个
<iframe src="iframe.html" id="iframe" onload="loader()" width="200" height="100"> </iframe>
<script>
var myVar
function loader(){
myVar = window.frames[0].window.test;
}
</script>
<a href="#" onclick='alert(myVar)' id="link">Click</a>
回答by emmmdeee
In the aside solution I just wanted to point out that when you use the jquery onload event handler function and you want to access the jquery "this" object you have to use the $(this.attr) notation.
在旁边的解决方案中,我只想指出,当您使用 jquery onload 事件处理函数并且想要访问 jquery“this”对象时,您必须使用 $(this.attr) 表示法。

