Best way to break out of a frame

J

Jonas Smith

When I started my site a long time ago, I found out that some other site
was framing my site and serving ads around it, even though I had no ads.

At the time as a solution (and it worked) I added the following
Javascript statement to all my html files:

<body onload="if (window != window.top) {top.location.href =
location.href;}">

That surely breaks frames.

However, recently, few of my users have complained that the site keeps
reloading when accessed with a blackberry. All pages keep reloading
until stopped. I found the culprit to be the above JS statement.

I'm not a programmer by any stretch of the imagination so I have no clue
how to fix the behaviour for them.

I tried:

if (window != window.top) {top.location.href = location.href;} else {
alert('we are top');}

but I never seen the alert on any browser even when the window contained
no framing. So it seems that the window != window.top is always
evaluating to true.

I appreciate any clues or pointers to fix this issue.

TIA
 
D

David Mark

When I started my site a long time ago, I found out that some other site
was framing my site and serving ads around it, even though I had no ads.

At the time as a solution (and it worked) I added the following
Javascript statement to all my html files:

<body onload="if (window != window.top) {top.location.href =
location.href;}">

That surely breaks frames.

And the back button. Call window.top.location.replace instead. Also,
fix the implied global (top.)
However, recently, few of my users have complained that the site keeps
reloading when accessed with a blackberry. All pages keep reloading
until stopped. I found the culprit to be the above JS statement.

And what did window.top evaluate to on this agent? My guess is
undefined.
I'm not a programmer by any stretch of the imagination so I have no clue
how to fix the behaviour for them.

I tried:

if (window != window.top) {top.location.href = location.href;} else {
alert('we are top');}

but I never seen the alert on any browser even when the window contained

Assuming this code is executed (BTW, use window.alert), you will
either navigate or see an alert.
 
D

David Mark

And the back button.  Call window.top.location.replace instead.  Also,
fix the implied global (top.)




And what did window.top evaluate to on this agent?  My guess is
undefined.

Looking again, that was a pretty lousy guess. Regardless, add an
alert(window.top) and see what the Blackberry says.
 
D

David Mark

When I started my site a long time ago, I found out that some other site
was framing my site and serving ads around it, even though I had no ads.

At the time as a solution (and it worked) I added the following
Javascript statement to all my html files:

<body onload="if (window != window.top) {top.location.href =
location.href;}">

That surely breaks frames.

However, recently, few of my users have complained that the site keeps
reloading when accessed with a blackberry. All pages keep reloading
until stopped. I found the culprit to be the above JS statement.

Here somebody has regurgitated a lot of bad examples in a very bad
color scheme, but there are a couple of useful ideas to be found:

http://perishablepress.com/press/2006/08/29/html-frames-notes-plus/

Does the Blackberry even support frames? Perhaps it would be useful
to skip the test if window.frames is not a truthy object.

The other thing that escaped me before is window.self. Try comparing
that to window.top instead.
 
J

Jonas Smith

Here somebody has regurgitated a lot of bad examples in a very bad
color scheme, but there are a couple of useful ideas to be found:

http://perishablepress.com/press/2006/08/29/html-frames-notes-plus/

Does the Blackberry even support frames? Perhaps it would be useful
to skip the test if window.frames is not a truthy object.

The other thing that escaped me before is window.self. Try comparing
that to window.top instead.

Thanks for the pointers, I made some changes and I am now waiting for my
volunteer tester to come back with results.
 
D

David Mark

To restore balance to the world Jonas Smith  wrote in
[email protected]




<script><!--
if (top.location != location) top.location.href = location.href;
//--></script>

You broke the back button (among other things.)

And what makes you think this will work any better?
 
T

Thomas 'PointedEars' Lahn

Joker7 said:
[...] David Mark wrote [...]:
[...] Jonas Smith wrote [...]:
I tried:
if (window != window.top) {top.location.href = location.href;}
else { alert('we are top');}
but I never seen the alert on any browser even when the window
contained no framing. So it seems that the window != window.top is
always evaluating to true.
[...]
<script><!--
if (top.location != location) top.location.href = location.href;
//--></script>
You broke the back button (among other things.)

And what makes you think this will work any better?

If you don't like it don't use it.......................................

It is not a matter of taste, but of functionality. Your solution does not
provide greater a functionality than the approach already taken; in fact, it
has the potential to provide less functionality than that as it introduces a
dependency on another proprietary property.

Your lengthy attribution novel and excessive quoting is not appreciated.
Also, you are crossposting to a dead group, alt.comp.lang.javascript. Yes,
I know ... "if you don't like it, don't read it". I intend to; the only
question is whether you would really like to go unnoticed.


Score adjusted

PointedEars
 
D

David Mark

To restore balance to the world David Mark  wrote in
(e-mail address removed)






If you don't like it don't use it.......................................

That goes without saying. What does not go without saying is that
your "solution" is a waste of other peoples' time.
 
J

Jonas Smith

Thanks for the pointers, I made some changes and I am now waiting for my
volunteer tester to come back with results.

I'm now using:

<body onLoad="if (window.self!=window.top) {window.top.location =
window.self.location;}">

That seems to work with the blackberry browser without reloading.
 
D

David Mark

I'm now using:

<body onLoad="if (window.self!=window.top) {window.top.location =
window.self.location;}">

You are still breaking the back button! That is the single most
annoying blunder that a JS developer can make (see any of Google's
properties for lots of bad examples.) Use location.replace to avert
this problem.
 
J

Jorge

I'm now using:

<body onLoad="if (window.self!=window.top) {window.top.location =
window.self.location;}">

That seems to work with the blackberry browser without reloading.

But why window.self ?

window.self.location === window.window.location === window.location

Or not ?

--Jorge.
 
D

David Mark

But why window.self ?

The OP seems to have settled on a pattern that works by coincidence.

This seems like a practical solution.

<body onload="if (window.self != window.top)
{ window.top.location.replace(window.location.href); }">

Of course, the location.replace method should be feature tested.

Also, there would seem no need to do this test at all if there is no
window.frames or window.frames.length is zero.
 
J

Jorge

Also, there would seem no need to do this test at all if there is no
window.frames or window.frames.length is zero.

window.frames or window.top.frames ?

--Jorge.
 
D

David Mark

window.frames or window.top.frames ?

--Jorge.

You could test if window.frames is a truthy object, and
window.frames.length is a number. A more direct test would also check
that window.top.frames.length is non-zero.
 
J

Jonas Smith

You are still breaking the back button! That is the single most
annoying blunder that a JS developer can make (see any of Google's
properties for lots of bad examples.) Use location.replace to avert
this problem.

I guess you didn't read the whole thread from the start. The reason for
this little tidbit of code in each of my pages was to break out of a
frame whenever somebody else framed my site within their own and serving
ads along side my pages.

Do you think that I would care about the back button when that happens?
I couldn't care less if people could get back to the asshole site.
 
J

Jonas Smith

But why window.self ?

window.self.location === window.window.location === window.location

Or not ?

Maybe. I already admitted to being a lousy programmer. The code that I
settled on works on Safari, Firefox, IE, Opera iPhone (Mobile Safari)
and doesn't cause blackberry devices to reload pages constantly.

I'm happy to have found a solution that works, even if it may not be the
best technically.
 
T

Thomas 'PointedEars' Lahn

Jonas said:
[...] David Mark wrote [...]:
On Mon, 28 Jul 2008 01:28:02 -0400, Jonas Smith wrote [...]:
Thanks for the pointers, I made some changes and I am now waiting
for my volunteer tester to come back with results.
I'm now using:

<body onLoad="if (window.self!=window.top) {window.top.location =
window.self.location;}">
You are still breaking the back button! That is the single most
annoying blunder that a JS developer can make (see any of Google's
properties for lots of bad examples.) Use location.replace to avert
this problem.

I guess you didn't read the whole thread from the start. The reason for
this little tidbit of code in each of my pages was to break out of a
frame whenever somebody else framed my site within their own and serving
ads along side my pages.

Do you think that I would care about the back button when that happens?
I couldn't care less if people could get back to the asshole site.

Your reasoning is flawed. You would be punishing the innocent user who
happened to navigate to the offender's site (let us keep street language on
the streets, shall we?), not the offender themselves. Because if the user
activated the Back button on your site after being redirected there, they
would be getting back to the offender's site, and shortly after back to your
site again. And, in fact, if you used window.top.location.replace()
instead, as suggested, you could prevent the user from seeing the offender's
site again when navigating back, while still preserving the display of your
Web site as you want it to be (provided client-side script support is enabled).

BTW, alt.comp.lang.javascript is a dead newsgroup, please do not crosspost
to there.


PointedEars
 
D

David Mark

I guess you didn't read the whole thread from the start. The reason for

You guessed wrong.
this little tidbit of code in each of my pages was to break out of a
frame whenever somebody else framed my site within their own and serving
ads along side my pages.

No kidding.
Do you think that I would care about the back button when that happens?

You? No. Competent Web developers? Yes.
I couldn't care less if people could get back to the asshole site.

Fix it the way I said and they won't.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top