从嵌入的 iframe 中获取全局变量

发布于 2024-11-07 12:14:24 字数 768 浏览 0 评论 0原文

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

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

现在该页面从另一个页面加载到 iframe 中...

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

从父页面,我想从 Page 获取全局“vara”的值.htm。

以下行均无效...

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

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

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

任何帮助将不胜感激!

更新

在更多地查看这个问题并尝试了可用于解决此问题的各种库之后,我顿悟到,由于我对主嵌入网站和 iframe 嵌入网站都有 IIS 控制,所以我可以在跨域脚本规则内工作对各个网站使用相同的基本 URL。此外,随着浏览器供应商通过不断更新加强安全性,解决该问题的库似乎需要不断更新。必须始终更新解决方法来规避浏览器试图阻止您做的事情,这确实是一件苦差事。

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

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

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

<iframe id="child_frame" src="http://mysite.com/Page.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!

UPDATE

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆光飞翔i 2024-11-14 12:14:24

如果您位于相同的域上,您的第一个语法应该可以工作。

如果 IFRAME 正在从另一个域加载页面,那么您将无法访问它。

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

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

相权↑美人 2024-11-14 12:14:24

此:

<iframe id="child_frame" src="http://" />

应该:

<iframe name="child_frame" src="http://" />

iframe 属性“id”应更改为“name”。

This:

<iframe id="child_frame" src="http://" />

Should be:

<iframe name="child_frame" src="http://" />

The iframe attribute "id" should be changed to "name".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文