By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,569 Members | 1,181 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,569 IT Pros & Developers. It's quick & easy.

Coding Problem

P: n/a
I need to find a javascript that I can put into the header of
a page so it does not allow the page the be contained in a frame.

Can anyone suggest how to go about doing this?


Fred

Jul 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Fred Atkinson wrote:
I need to find a javascript that I can put into the header of
a page so it does not allow the page the be contained in a frame.

Can anyone suggest how to go about doing this?


if (parent.frames.length) top.location = location.href;
JW

Jul 20 '05 #2

P: n/a
On Thu, 1 Jan 2004 16:31:20 +0100, "Janwillem Borleffs"
<jw@jwscripts.com> wrote:
Fred Atkinson wrote:
I need to find a javascript that I can put into the header of
a page so it does not allow the page the be contained in a frame.

Can anyone suggest how to go about doing this?


if (parent.frames.length) top.location = location.href;
JW

I put the following in the header of the page:

<SCRIPT TYPE="text/javascript>"
if (parent.frames.length) top.location = location.href
// -->
</SCRIPT>

Unfortunately, my page still appears trapped in a frame when
someone tries to use it that way appearing to show their URL as my
page.

In reading my original post, I can see I didn't make myself
especially clear. So let me explain the scenario.

I have a number of other pages linking to me. These pages
were written as a result of an article that I wrote about using a free
page and making a URL out of it by registering a domain name, putting
the free page into a frame, and listing that free page in the frame.

The problem was that when someone clicks on your links that
they are forwarded to the new page sitll locked in a frame.

These folks don't follow the part of my instructions where I l
them to put 'target=top' in their links so they break out of the frame
when they click. Thus, they could be on any site and still be showing
a different URL in the address bar.

These folks typically put a link to my article on their page.
Of course, my page comes up with their URL in the address bar.

What I want is that if my page comes up in a frame, that it
should immediately forward to my URL so that my URL appears in the
address field with my content, not my content with their URL.

Thanks,
Fred

Jul 20 '05 #3

P: n/a
Fred Atkinson wrote:
I put the following in the header of the page:

<SCRIPT TYPE="text/javascript>"
if (parent.frames.length) top.location = location.href
// -->
</SCRIPT>


Try this:

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

However, this will probably cause an access denied error because the frame
that encapsules your page doesn't reside on your domain.

Here are two ways of handling this:

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

Or:

try {
if (top.location.href != location.href) top.location =
location.href;
} catch (e) {
top.location = location.href;
}
HTH,
JW


Jul 20 '05 #4

P: n/a
Fred Atkinson wrote on 01 jan 2004 in comp.lang.javascript:
I put the following in the header of the page:

<SCRIPT TYPE="text/javascript>"
if (parent.frames.length) top.location = location.href
// -->
</SCRIPT>


There are two mistakes in this code

1
<SCRIPT TYPE="text/javascript>"

should be

<SCRIPT TYPE="text/javascript">

2
a closing

// -->

should only be used if there is also a starting string, but why use it?
==============

Try:

<SCRIPT>
if (top != self) top.location.href = self.location.href;
</SCRIPT>

Read the problems in:

<http://www.quirksmode.org/js/framebust.html>


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #5

P: n/a
Janwillem Borleffs wrote:
Fred Atkinson wrote:
I need to find a javascript that I can put into the header of
a page so it does not allow the page the be contained in a frame.

Can anyone suggest how to go about doing this?


if (parent.frames.length) top.location = location.href;


Too complicated and error-prone. You check for the number of frames
in the parent frameset, but replace the top frameset. Try this:

if (self != top)
{
top.location = self.location;
}

You could also replace *all* occurrences of "top" with "parent".
PointedEars
Jul 20 '05 #6

P: n/a
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
if (self != top)
{
top.location = self.location;
I prefer
top.location.href = self.location.href;
Less magic involved, although I don't think there is a browser
where it makes a difference.
}

You could also replace *all* occurrences of "top" with "parent".


.... but if the same code was run again when the page is loaded in the
parent, it will end up in top anyway.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7

P: n/a
On Sun, 11 Jan 2004 20:22:18 +0100, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
Less magic involved, although I don't think there is a browser
where it makes a difference.


It's my recollection that there is... (it'll generate a runtime error
if top.location is not defined or not an object)

Of course I guess it's now incumbment on me remembering which it was,
maybe if I mentioned it in the group.

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.