Forum Moderators: open
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?
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 ****.
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]
[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.
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.
Note that ".replace()" will not work in Netscape 6 when the top is a remote site but NS6 is a rare bird these days.
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?
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.