views:

796

answers:

4

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?

+1  A: 

Its imposable, 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"&gt;
</iframe>

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

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.

jishi
document.domain only allows you to "move up" from your current domain. E.g. from 'sub.domain.example.com' or 'sub.example.com' to "example.com". However you can't set it to '.com'. Additionally, you can only set the value once.
Josh
A: 

It looks like this guy got cross domain JavaScript working between iframes.

Josh
A: 

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().

lacker