简体   繁体   English

JavaScript:从iframe关闭窗口

[英]JavaScript: closing window from iframe

I have a page P1 loading from site S1 which contains an iframe. 我有一个从站点S1加载的页面P1,其中包含iframe。 That iframe loads a page P2 from another site S2. 该iframe从另一个站点S2加载页面P2。 At some point P2 would like to close the browser window, which contains P1 loaded from S1. 在某个时候,P2想要关闭浏览器窗口,其中包含从S1加载的P1。 Of course, since P2 is loaded from another site, it can't just do parent.close(). 当然,由于P2是从另一个站点加载的,因此不能仅执行parent.close()。

I have full control over P1 and P2, so I can add JavaScript code to both P1 and P2 as needed. 我可以完全控制P1和P2,因此可以根据需要将JavaScript代码添加到P1和P2中。 Suggestions on how to resolve this? 有关如何解决此问题的建议?

It's impossible, I am afraid. 恐怕这是不可能的。 JavaScript from an iframe that is loaded to a different site then the one it is being rendered on is strictly prohibited due to security issues. 由于安全问题,严格禁止将iframe中的JavaScript加载到另一个网站上,然后再将其呈现在另一个网站上。

However, if the iframe is pointed to the same site you can get to it like: 但是,如果将iframe指向同一个网站,则可以使用以下方法:

<iframe name = "frame1" src = "http://yoursite">
</iframe>

<script type = "text/javascript">
    alert(window.frames["frame1"].document);
</script>

If they originated from the same domain, you can modify the security-restrictions to allow modification between sub-domains. 如果它们源自同一域,则可以修改安全限制,以允许在子域之间进行修改。

set document.domain = "domain.com"; 设置document.domain =“ domain.com”; //on both pages and they are allowed to modify eachother. //在两个页面上,都可以互相修改。

It might work to just set them to a bogus-domain, haven't tried that, or just simply ".com" or something. 仅将它们设置为伪域名,没有尝试过,或者只是简单地使用“ .com”或类似内容,可能会起作用。

看来这个家伙在iframe之间使用了跨域JavaScript。

You can use Flash to do this. 您可以使用Flash来执行此操作。 Send the user to a new top-level page that you control with a URLRequest with a _top target, and have that page contain javascript that does a window.close(). 将用户发送到您使用_top目标用URLRequest控制的新顶层页面,并使该页面包含执行window.close()的javascript。

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

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