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.
It looks like this guy got cross domain JavaScript working between iframes.
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().