将 javascript 变量从 iframe 传递到父框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6271313/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:06:51  来源:igfitidea点击:

pass a javascript variable from an iframe to the parent frame

javascript

提问by zach

So I have a variable in my iframe like so:

所以我的 iframe 中有一个变量,如下所示:

 <script>
var zipphone = "<?= $phone ?>";
 </script>

Now I want to pass that variable to the parent frame after the iframe is loaded. What is the simplest way to do that?

现在我想在加载 iframe 后将该变量传递给父框架。最简单的方法是什么?

回答by icktoofay

If the pages are both on the same domain, you could call a function of the parent window:

如果页面都在同一个域中,则可以调用父窗口的函数:

window.parent.zipPhoneCallback(zipphone);

In the parent window, you could define a function like this:

在父窗口中,您可以定义这样的函数:

function zipPhoneCallback(zipphone) {
    ((console&&console.log)||alert)("zipphone = " + zipphone);
}

回答by Peter Dow

Beware there seems to be a Chrome browser issue with addressing variables from webpages to or from IFRAMES.This issue appears in offline testing.

请注意,从网页到 IFRAMES 或从 IFRAMES 寻址变量似乎存在Chrome 浏览器问题。此问题出现在离线测试中。

That aside, assuming your browser actually implements basic functions of Javascript, you address your variable from your main webpage using window.myiframename.zipphone

除此之外,假设您的浏览器实际上实现了 Javascript 的基本功能,您可以使用 window.myiframename.zipphone 从主网页中寻址您的变量

<IFRAME NAME="myiframename" SRC="myzipphoneiframefile.htm" ....

<script type="text/javascript">
<!--
// then whatever you do with your variable 
// read it
var z = window.myiframename.zipphone;
// write to it
window.myiframename.zipphone = "....";

and so on.

等等。

Example.

例子。

DOC1.HTM contents -

DOC1.HTM 内容 -

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<title>DOC1.HTM</title> 
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> 
</head> 
<body bgcolor=pink> 

<h1>DOC1.HTM</h1> 

<iframe name="iframename" src="doc2.htm"></iframe> 
<p> 
<br /> 
<a href="#" onclick="alert('The value of the variable test_var set by a script in DOC2.HTM embedded in the DOC1.HTM page by an iframe named IFRAMENAME as read from a script running in DOC1.HTM appears to be - ' + window.iframename.test_var);return false;">check variable</a> 
</p> 

</body> 
</html> 

DOC2.HTM contents -

DOC2.HTM 内容 -

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<title>DOC2.HTM</title> 
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> 
</head> 

<body bgcolor=red> 

<script type="text/javascript"> 

var test_var="testing, testing . . ."; 

</script> 


<h1>DOC2.HTM</h1> 
</body> 
</html> 

That works beautifully even with older versions of Internet Explorer but try it when offline testing with Chrome and window.iframename.test_var appears to be undefined because of the Chrome issue.

即使使用旧版本的 Internet Explorer,它也能很好地工作,但在使用 Chrome 进行离线测试时尝试它,并且 window.iframename.test_var 由于 Chrome 问题似乎未定义。

Anyway, look out for future versions of Chrome fixing this because it is a lot of egg on Google's face while they haven't.

无论如何,请留意 Chrome 的未来版本来解决这个问题,因为谷歌的脸上有很多鸡蛋,而他们还没有。

I have a work-around for this issue in Chrome offline testing.

我在 Chrome 离线测试中解决了这个问题。

Thanks to Lucy24 on WebmasterWorld for helping. http://www.webmasterworld.com/google_chrome/4689258.htm#msg4689342

感谢 WebmasterWorld 上的 Lucy24 的帮助。http://www.webmasterworld.com/google_chrome/4689258.htm#msg4689342

This issue with Chrome arose when my javascript was being tested off lineand files doc1.htm and doc2.htm are in the same folder on my PC.

当我的 javascript 被离线测试并且文件 doc1.htm 和 doc2.htm 在我的 PC 上的同一个文件夹中时,Chrome 出现了这个问题。

Moving the doc1.htm & doc2.htm files to a folder where I test my server side php programs, which runs using Windows Internet Services Manager means I can address the files using h t t p : / / l o c a l h o s t addresses and bingo, Chrome behaves as it should have behaved in offline mode.

将 doc1.htm 和 doc2.htm 文件移动到我测试服务器端 php 程序的文件夹中,该程序使用 Windows Internet 服务管理器运行,这意味着我可以使用 http://localhost 地址和宾果游戏来处理这些文件,Chrome 的行为应该如此在离线模式下表现。

It is not "legitimate" in my opinion for Chrome's javascript not to be able to directly address files in the same offline folder.

在我看来,Chrome 的 javascript 无法直接处理同一脱机文件夹中的文件,这不是“合法的”。

There's absolutely no security issue when you are running your own javascript files on your own PC. Or if Chrome wanted to offer a security setting that allowed or disallowed offline IFRAME variable scoping then OK, that would be fine.

当您在自己的 PC 上运行自己的 javascript 文件时,绝对没有安全问题。或者,如果 Chrome 想要提供允许或禁止离线 IFRAME 变量作用域的安全设置,那么没问题。

The error message is not at all clear I submit and Lucy24 did well to figure out what it meant.

我提交的错误消息根本不清楚,Lucy24 很好地弄清楚了它的含义。

If Internet Explorer and Firefox etc allow you to test your javascript offline then why not Chrome?

如果 Internet Explorer 和 Firefox 等允许您离线测试您的 javascript,那么为什么不 Chrome 呢?

So well done Lucy24. Not so well done Chrome.

太好了,露西24。Chrome 做得不是很好。