Forum Moderators: open

Message Too Old, No Replies

What's the best frame-breaker?

         

encyclo

7:04 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was wondering, I use a little break-out-of-frames script on every page of my sites to ensure that it won't appear inside someone elses frameset on another site. There are several versions flying around, such as:

if (parent.frames.length > 0) {
parent.location.href = self.document.location
}

or also:

if( window!= window.top ) {
top.location.href = location.href;
}

Obviously, I want a script which won't break the back button, works everywhere and is compact.

My question is: which script the best way of breaking out of a frameset?

wruppert

8:01 pm on Dec 2, 2004 (gmt 0)

10+ Year Member



Here is what I use. I'm also curious what the best one is.

if (top!= self) top.location=self.document.location;

Bernard Marx

8:29 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



document.location is deprecated, so stick with window.location .
(or just location).

self, and window are both aliases for the host/window.
'this' will also point to the window when the execution context is global.

parent & top will also point to the window, except when lower down in a frame hierarchy.
This is just what those test like this does:

if(top!= self)

You don't need to compare locations, just object references.
Apart from that...it's all the same ****.

nancyb

8:47 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



this is what I was using:

if(top.location!= document.location)
top.location = document.location;

but your response is Greek to me Bernard Marx, I'm a java script dunce ;)

would you mind showing the entire snip of code?

Bernard Marx

9:01 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's much the same as some of the others:

if(top!= self) top.location.href = self.location.href

The only real difference between all of them is that one like ..er.. yours compares the locations. This isn't necessary (and it wastes..ooh, must be 2ms ). All we need to do is what we actually want.

"Am I the top object? (or is it another object)"

if(self!= top)

Comparing the locations only does this by implication.
There is the ridiculously unlikely possiblity, in fact, that your page could end up framed inside itself. In this case, comparing objects would work, while comparing locations would fail.

BTW, has anyone tried framing a page inside itself?
Wouldn't the browser end up crashing?

[edited by: DrDoc at 1:11 am (utc) on Dec. 3, 2004]
[edit reason] fixed typo [/edit]

wruppert

10:18 pm on Dec 2, 2004 (gmt 0)

10+ Year Member



I don't know Javascript. Given that document.location is deprecated, what is the correct statement?

Bernard seems to imply:


top.location = location

Is that correct?

nancyb

10:26 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tried Bernard's and it doesn't break out of abo-t . com's frame, but the one I posted above in msg #4 does break out of their frame.

Is abou t . com's site doing something different than most pages that would capture another page inside a frame?

kaled

11:28 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you use frames yourself, it is a little more complicated, however, I would recommend this.

[b]if (top!= self) top.location.replace(self.location.href);[/b]

If you assign to location directly (rather than use replace) pressing the back button will cause you to bounce straight back again - you need to hit it twice.

If you want to be certain that it will work on ancient browsers and modern, you could try something like this.

[b]function breakFree() {
if (top.location) top.location.replace(self.location.href)
else top.document.location.replace(self.document.location.href);
}
if (top!= self) breakFree();[/b]

Kaled.

nancyb

12:45 am on Dec 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



kaled, I'm not sure if your example is just for a framed site that ends up in someone else's frame or if your example would work for either a framed or non-framed site.

my site is not framed, but Bernard's code didn't break out of abou t . com site's frame of my pages. Don't really mind being in about's frame because they give a way to break out, but I don't want to be framed by someone else.

DrDoc

1:11 am on Dec 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's because of a typo in Bernard's code...
kaled's or Bernard's code should otherwise work fine...

nancyb

1:20 am on Dec 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



oh doh, took me a minute even after you pointed out the typo.

thx to all!

kaled

9:47 am on Dec 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My example is suitable for unframed sites. It may also be placed in a frameset I think (but I've never tried that).

DO NOT use it in a child frame of a frameset.

Kaled.

kwasher

9:38 pm on Dec 29, 2004 (gmt 0)

10+ Year Member



Is there a way to break your site out of frames using mod_rewrite in .htaccess?

RonPK

11:58 am on Dec 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't think so, kwasher. Whether a page is framed or not can only be detected on the client's side.

amznVibe

2:10 pm on Jan 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This page brings up some points and code tips to frame breaking that some may not have thought about:
[xs4all.nl...]
(hit CANCEL on the "go to new site" prompt)

Note that ".replace()" will not work in Netscape 6 when the top is a remote site but NS6 is a rare bird these days.

Lorel

5:09 pm on Feb 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I just discovered someone that had one of my pages in frames - a page I just redesigned and it hit #1 in Google SERPs - grrrrrr! and witthin 3 days I found my page hijacked with a redirect script and also in a frame. The irritating thing is i asked to have this page submitted to their directory which looks harmless but they must have my page also set up to display in a frame linked from somewhere else (probably from the site map which I haven't looked for yet). I wrote the owner of the site with no response so now I'm writing the host but in the meantime I wanted to get it out of that frame.

I tried the scripts mentioned above. Kaled's did not work but Bernard's did. I have a Mac OS 10x using IE 5.2 for the Mac, so wondering it that's why Kaled's didn't work for me? I'd rather use that script if it works on older browsers/systems.

Any suggestions?

Bernard Marx

5:41 pm on Feb 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only reasons I can think of that Kaled's wouldn't work are:

1. Browser support for the replace method.
It's been around for a good while. Does IE Mac 5 support it? Dunno.

2. The cross-domain scripting issue.
Browsers generally allow read/write access to location.href, but some may perhaps draw up the bars when it comes to calling a method, such as replace.

replace is, as Kaled says, much (more convenient ¦ less irritating) for the user.

Perhaps some combination of browser sniffing and try..catch block may result in the best available approach being used.

kaled

6:21 pm on Feb 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try the following


if (top!= self)
if (location) top.location.replace(self.location.href)
else top.document.location.replace(self.document.location.href);

The problem may be caused by a change to the document object model.

Kaled.

Lorel

11:56 pm on Feb 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks Kaled but that didn't work either-it stayed inside the frame.