简体   繁体   English

iframe中的Javascript window.opener

[英]Javascript window.opener in iframe

I am trying to access the opener of a popup using the window.opener reference in the popup's script file. 我正在尝试使用弹出窗口的脚本文件中的window.opener引用访问弹出窗口的打开器。

Consider the following script included in the popup.html: 考虑以下popup.html中包含的脚本:

http ://localhost/test/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: 当不涉及iframe时,此方法有效,我们将从popup.html中看到警报消息:

http ://localhost/test/base.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. 当有3个文件时,就会出现问题。.带有iframe的容器页面会从该iframe中启动弹出窗口。 This does not show the alert message when popup appears: 当弹出窗口出现时,这不会显示警报消息:

C:\\TestContainer\\container.html 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. AFAIK base.html和popup.html在同一域中,因此我认为没有理由。 Are there other ways to access the opener via other DOM attributes in the popup.html script? 还有其他方法可以通过popup.html脚本中的其他DOM属性访问打开器吗? 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. 除了对opener的引用,其他所有内容似乎都可以在所有脚本中正常工作。

Using IE 11. 使用IE 11。

Any assistance is appreciated! 任何帮助表示赞赏!

An iframe does not have a window.opener . iframe没有window.opener It has a window.parent (the window in which the iframe is embedded). 它具有window.parent (嵌入iframe的窗口)。 If the parent is indeed the top level window, then perhaps that window has a window.opener if its the actual popup window. 如果父级确实是顶级窗口,则该窗口可能具有window.opener如果它是实际的弹出窗口)。

You haven't shown your specific HTML situation, but perhaps what you want from the iframe is this: 您尚未显示特定的HTML情况,但是也许从iframe中想要的是这样的:

window.parent.opener

I came up with a workaround to this problem. 我想出了解决此问题的方法。 In my situation, the popup was very simple, so what I did was embed its html in a hidden div in the base page. 在我的情况下,弹出窗口非常简单,因此我所做的就是将其html嵌入到基础页面的隐藏div中。 Then, when the popup was needed, I simply made the window show the contents of the hidden html. 然后,当需要弹出窗口时,我只是使窗口显示隐藏的html的内容。

Unfortunately, the event processing for the buttons found in the popup do not automatically get set when this is done, so I had to manually set them using addEventListener once the window was shown (ie, putting 'onclick' events in the html tags will not work). 不幸的是,完成此操作后,不会自动设置在弹出窗口中找到的按钮的事件处理,因此一旦显示窗口,我就必须使用addEventListener手动设置它们(即,将'onclick'事件放入html标签中不会工作)。

   var popup = window.open("", "_blank", "..", false);
   var popup_html = window.document.getElementById('hidden_popup_contents').innerHTML;
   popup.document.writeln(popup_html);
   popup.document.getElementById('popup_button').addEventListener('click', SomeFunction);

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

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