从嵌入式iframe中获取全局变量 [英] grab global variable from an embedded iframe

查看:419
本文介绍了从嵌入式iframe中获取全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说HTML页面(Page.htm)包含以下内容...

say an HTML page (Page.htm) contains the following...

<script type="text/javascript">
    var vara = 'varA';
</script>

现在这个页面被加载到另一个页面的iframe中......

Now this page is loaded into an iframe from another page with...

<iframe id="child_frame" src="http://mysite.com/Page.htm" />

从父页面,我想从Page获得全局'vara'的值。 htm。

From the parent page, I would like to get the value of the global 'vara' from Page.htm.

以下所有行都不起作用......

None of the following lines work...

    window.frames['child_frame'].window.vara;

    window.frames['child_frame'].window['vara'];

    window.frames['child_frame'].contentWindow['vara']; 
    // in fact contentWindow returns undefined!!

任何帮助都将不胜感激!

Any help would be appreciated!

更新

在更多地查看此问题并尝试各种可用于解决此问题的库之后,我有了顿悟,因为我有IIS控制两个主要和iframe嵌入式网站,我可以通过使用相同的基本URL为各种网站在跨域脚本编写规则。此外,解决问题的库似乎需要不断更新,因为浏览器供应商通过持续更新来加强其安全性。为了避免浏览器试图阻止你做什么,必须始终更新解决方法真的是一件苦差事。

After looking at this problem more, and trying the various libraries that are available to get around this issue, I had the epiphany that since I have IIS control over both the main and iframe embedded websites, I can work within the cross domain scripting rules by using the same base URL for the various websites. Also, the libraries that get around the problem appear to need constant updating as the browser vendors tighten their security with continuous updates. It would really be a chore to have to always be updating the workaround for circumventing what the browser is trying to keep you from doing.

推荐答案

你的第一个语法应该是 IF 你在相同的doamin

Your first syntax should work IF you're on the same doamin.

如果 IFRAME 正在从其他域加载页面,然后您将无法访问该页面。

If the IFRAME is loading a page from another domain then you won't be able to access it.

这篇关于从嵌入式iframe中获取全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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