Jayrox
Jayrox

Reputation: 4375

Break from frames based on parent's URL

I am trying to help a friend of mine break his site out of some nasty frames: example. This wouldn't normally be an issue but this particular site crosses some ethical boundaries by placing an advertisement on the top of the page that is NOT paying my friend.

Here is where it gets complicated, he would like his site to be in frames for certain sites like netvibes.

So, I tried something like this:

<!-- start break from getweb.info -->
<script type="text/javascript">

    purl = parent.location.href;
    //alert(purl);
    if(purl.indexOf('getweb') > 0 && top.frames.length!=0)
    {
        top.location=self.document.location;
    }
</script>
<!-- end break from getweb.info -->

However, because it needs to be a conditional break, this isn't working because firefox.(assuming others have the same error also, untested yet) gives an error stating that the site in the frame is not permitted to get the location.href property of the parent frame.

Is there a way for JavaScript to break out of a frame based on the parent frame's location?

Upvotes: 0

Views: 1593

Answers (1)

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143234

You're running into the same origin policy. You can't see the location (or any other proerties) of a frame with a different origin domain.

You can, however, see your own document.referrer. If you're framed then the referrer should be the URL of the containing frame.

if (window.self != window.top && document.referrer.indexOf('getweb') > 0 ) {
  top.location.replace(window.location.pathname);
}

This isn't bulletproof. The framing site could use an intermediary (like netvibes), for example. It might be annoying enough for them to stop framing your friend's site, however.

Upvotes: 2

Related Questions