views:

273

answers:

1

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?

+2  A: 

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.

Laurence Gonsalves
You may want to check if document.referrer is empty in case the vistor typed the address directly, or used a bookmark to go to the website.
Julien
If document.referrer is empty then document.referrer.indexOf('getweb') will be -1, so it'll behave as it should.
Laurence Gonsalves