在iframe中的Javascript window.opener [英] Javascript window.opener in iframe
问题描述
我正在使用弹出窗口脚本文件中的window.opener引用来访问弹出窗口的开启者。
请考虑popup.html中包含的以下脚本:
http:// localhost / test / .html
< script>
alert(window.opener.test_dom);
< / script>
当没有iframe涉及时,这可以工作,我们将看到警报来自popup.html的消息:
http://localhost/test/base.html
< html>
...
< script>
var test_dom ='test_dom';
var popup = window.open(http://localhost/test/popup.html,...
< / script>
< / html>
当有3个文件时,存在问题..具有iframe的容器页面它会在该iframe内启动一个弹出窗口,弹出窗口不显示警报消息:
C:\TestContainer\container.html
< html>
...
< iframe src =http: //localhost/test/base.html\">
< script>
var test_dom ='test_dom';
var popup = window.open(http:// localhost / test /popup.html,...
< / script>
< iframe>
< / html>
也许这是一个安全限制?AFAIK base.html和popup.html在同一个域中,所以我看到没有理由这是应该的。有没有其他方法来访问开瓶器通过popup.html脚本中的其他DOM属性?
除了对开启者的引用之外,其他一切似乎都像预期一样在所有脚本中正常工作。
使用IE 11。
任何帮助都不胜感激!
iframe
没有 window.opener
。它有一个 window.parent
(嵌入iframe的窗口)。如果父级确实是顶级窗口,那么也许该窗口有一个 window.opener
如果它的实际弹出窗口。
您没有显示您的特定HTML情况,但也可能您从iframe中想要的是:
window.parent.opener
I am trying to access the opener of a popup using the window.opener reference in the popup's script file.
Consider the following script included in the popup.html:
http ://localhost/test/popup.html
<script> alert(window.opener.test_dom); </script>
This works when no iframe is involved, we will see the alert message from popup.html:
http ://localhost/test/base.html
<html> ... <script> var test_dom = 'test_dom'; var popup = window.open("http://localhost/test/popup.html",... </script> </html>
The problem exists when there are 3 files.. a container page with an iframe in it that launches a popup from within that iframe. This does not show the alert message when popup appears:
C:\TestContainer\container.html
<html> ... <iframe src="http://localhost/test/base.html"> <script> var test_dom = 'test_dom'; var popup = window.open("http://localhost/test/popup.html",... </script> <iframe> </html>
Perhaps this is a security restriction? AFAIK base.html and popup.html are in the same domain, so I see no reason why this should be. Are there other ways to access the opener via other DOM attributes in the popup.html script? I've been stuck on this for a day now.
Other than the reference to opener, everything else seems to work as expected in all scripts.
Using IE 11.
Any assistance is appreciated!
An iframe
does not have a window.opener
. It has a window.parent
(the window in which the iframe is embedded). If the parent is indeed the top level window, then perhaps that window has a window.opener
if its the actual popup window.
You haven't shown your specific HTML situation, but perhaps what you want from the iframe is this:
window.parent.opener
这篇关于在iframe中的Javascript window.opener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!