简体   繁体   English

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

[英]pass a javascript variable from an iframe to the parent frame

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.现在我想在加载 iframe 后将该变量传递给父框架。 What is the simplest way to do that?最简单的方法是什么?

If the pages are both on the same domain, you could call a function of the parent window:如果页面都在同一个域上,您可以调用父 window 的 function:

window.parent.zipPhoneCallback(zipphone);

In the parent window, you could define a function like this:在父 window 中,您可以像这样定义 function:

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

Beware there seems to bea Chrome browser issue with addressing variables from webpages to or from IFRAMES.请注意,从网页到 IFRAMES 或从 IFRAMES 寻址变量似乎存在 Chrome 浏览器问题。 This issue appears in offline testing.此问题出现在离线测试中。

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.感谢 WebmasterWorld 上的 Lucy24 提供的帮助。 http://www.webmasterworld.com/google_chrome/4689258.htm#msg4689342 http://www.webmasterworld.com/google_chrome/4689258.htm#msg4689342

This issue with Chrome arose when my javascript was being tested off line and 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 http: / / lo c alhost addresses and bingo, Chrome behaves as it should have behaved in offline mode. 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 http: / / lo c alhost addresses and bingo, Chrome behaves as它应该在离线模式下运行。

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.当您在自己的 PC 上运行自己的 javascript 文件时,绝对没有安全问题。 Or if Chrome wanted to offer a security setting that allowed or disallowed offline IFRAME variable scoping then OK, that would be fine.或者,如果 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.做得好露西24。 Not so well done Chrome. Chrome做得不太好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM