William said:
I did a little searching for javascript code that will make
a page "jump out" of frames when the page is loaded. There
seems to be many, many ways to do this and I've added the
ones I found to the end of this post.
Which one should I use? Is there a generally accepted
"best" way to do this that works will all browsers?
If you are breaking out of frames they will presumably be other people's
frames and cross-domain/same-origin security restrictions will apply.
The extent to which you can read properties of the containing frame may
be more or less restricted because of this.
It is important that your code does not cause an exception due to
security restrictions else it will not only not escape the frame, but it
might not work properly at all.
(top != self) and (top != window) are equivalent, as are (parent !=
self) and (parent != window). Top, self, window and parent are
properties of your global/window object so there should be no problem
reading them and comparing them.
top.location = self.location;
This might error because the - location - property refers to an object.
Although you can (and probably should) set it with a sting value this
assignment is setting it to an object reference. Probably the -
self.location - object reference will be type-converted to a sting for
the assignment, but I wouldn't want to trust to that in this context.
Instead assign the - href - (string) property of your location object to
the location property of the top or parent frame:-
top.location = self.location.href;
-or:-
parent.location = window.location.href;
(and related permutations)
- assigning to the - location - property in the parent/top frame is not
normally a security issue as at just unloads and replaces the entire
parent/top document.
}
if (window != top) {
top.location.href = location.href
Assigning to the href property of the - top.location - object might be
subject to security restrictions (you may be denied access to the
contents of top.location, or read access to top itself (you have to have
read access to resolve - top.location - to an object reference)).
if (top.window != window) {
top and top.window should refer to the same object, but trying to read
properties of top might be restricted.
top.location.href = window.location.href
}
This assignment is functionally equivalent to the previous example.
if (parent.frames.length > 0) {
Reading properties of the - parent - and/or - parent.frames - object may
be subject to security restrictions.
parent.location.href = location.href;
}
if (top.location != location) {
As indeed might comparing properties of the - top - or - parent - object
with properties of your own global/window object, if they are from
different domains.
top.location.href = document.location.href
}
if (parent.frames.length >= 1) {
Again, reading properties of the - parent - and/or - parent.frames -
object may be subject to security restrictions.
window.top.location.href = "index.html"
}
There is more maintenance in using string literals (different code on
each page and a need to update it if you decide to change the page name
(or copy and paste to create a new page). While not only do versions
that assign location.href not need to know their file name, they can all
be imported with a common site-wide JS file.
Compare the self or window properties of your global/window object with
its top or parent properties and if they differ assign the location.href
string from your global/window object to the location property of the
top or parent object. Something like:-
if(parent != window){
parent.location = location.href;
}
Richard.