简体   繁体   中英

JavaScript: closing window from iframe

I have a page P1 loading from site S1 which contains an iframe. That iframe loads a page P2 from another site S2. At some point P2 would like to close the browser window, which contains P1 loaded from S1. Of course, since P2 is loaded from another site, it can't just do parent.close().

I have full control over P1 and P2, so I can add JavaScript code to both P1 and P2 as needed. 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.

However, if the iframe is pointed to the same site you can get to it like:

<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"; //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.

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

You can use Flash to do this. 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().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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