从嵌入的 iframe 中获取全局变量
假设 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您位于相同的域上,您的第一个语法应该可以工作。
如果
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.此:
应该:
iframe 属性“id”应更改为“name”。
This:
Should be:
The iframe attribute "id" should be changed to "name".