determining if you're in a frame or an iframe

S

Stevo

Does anyone know of a property that I can look at to see if the page my
script finds itself in was created as a frame or an iframe in the parent
page? My code needs to behave differently whether it's in an iframe or a
frame.

I know how to do it when the parent is in the same domain. I can just
test if parent.document.body.tagName=='frameset' and that tells me that
I must be in a frame. If that test returns false then I'm in an iframe.

The problem comes when the page I'm on is in a different domain to the
parent. I can't access many properties of the parent window, so I can't
determine whether it's a frameset or not. I can look at parent.frames
and if I'm frame 0, I can even look at parent.frames[0].location.href
but that still doesn't help me determine if I'm in a frame or iframe.

I need to know so that I can determine whether the content I want to
write should be written into the parent page or the current page. If the
parent is a frameset then I write into this current window. If the
parent is not a frameset, then I write into the parent window.
 
G

Grga

I had a similar problem a few days ago. It seems that you can't access the
properties and methods of a parent window if your frame was created with an
URL that is not on the same domain. I found a solution to this problem by
creating a frame that held a page on the original domain, and used that page
to redirect via PHP (<?php header("Location: www.google.com"); ?>).

Cheers,
Grga
 
S

Stevo

Grga said:
I found a solution to this problem by creating a frame that held a page on the original domain, and used that page
to redirect via PHP (<?php header("Location: www.google.com"); ?>).

Thanks but unfortunately that doesn't help. I don't control the page
that creates the frame. My script just finds itself included into either
a frame or an iframe, and needs to find out which it is.
 
D

Dylan Parry

Stevo said:
Thanks but unfortunately that doesn't help. I don't control the page
that creates the frame. My script just finds itself included into either
a frame or an iframe, and needs to find out which it is.

Could you do it with URL parameters?

What I mean is, have "?type=iframe" or "?type=frame" as part of the URL
and get people that reference your script to use the appropriate URL for
the frame content. That way you could retrieve the parameter and tell
what type of frame it is /supposed/ to be at least.

--
Dylan Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.
 
S

Stevo

Dylan said:
Could you do it with URL parameters?
What I mean is, have "?type=iframe" or "?type=frame" as part of the URL

It's a nice idea but there's no chance of me having that kind of
influence. At the moment I'm specifying that they include one version if
they're using it in a page that's in a frame, and a different one if
it's included in a page that's in an iframe. The scripts differ by only
one line, which basically sets a flag to true or false at the top of the
script as to whether or not it's in a frameset frame. If I update the
script and want to release a new version, it's just a 10 second edit and
a "save as" to generate the alternate version. I'd naturally like to
have a unified version which would end up being bigger due to the extra
logic I'd need to add, if only I could find some logic that can
determine where it's being used, frame or iframe.
 
H

Henry

Does anyone know of a property that I can look at to see if
the page my script finds itself in was created as a frame
or an iframe in the parent page?
<snip>

The - window - and - parent - properties will indicate that. If they
are the same object then the code is not running in a FRAME or an
IFRAME. If they are not the same object then it is in a FRAME or an
IFRAME.

if(window == parent){
//not in a frame/iframe
}else{
//in a frame/iframe
}

Cross domain security is normally not an issue with this type of
identity test as even if the - parent - is from a different domain you
are still not actually trying to access its contents at this point.
 
S

Stevo

Henry said:
The - window - and - parent - properties will indicate that. If they
are the same object then the code is not running in a FRAME or an
IFRAME. If they are not the same object then it is in a FRAME or an
IFRAME.
if(window == parent){
//not in a frame/iframe
}else{
//in a frame/iframe
}

I think you might have skipped to the end a bit too quickly. I know
already if I'm in the top page (like you said I can test window==parent,
or top==self).

The situation I'm struggling with is when *I AM* in either a frame or an
iframe, and I want to know which of those two it is. Is it "iframe" or
is it "frame".
 
D

Dylan Parry

Stevo said:
It's a nice idea but there's no chance of me having that kind of
influence. At the moment I'm specifying that they include one version if
they're using it in a page that's in a frame, and a different one if
it's included in a page that's in an iframe. The scripts differ by only
one line, which basically sets a flag to true or false at the top of the
script as to whether or not it's in a frameset frame. If I update the
script and want to release a new version, it's just a 10 second edit and
a "save as" to generate the alternate version. I'd naturally like to
have a unified version which would end up being bigger due to the extra
logic I'd need to add, if only I could find some logic that can
determine where it's being used, frame or iframe.

Okay, so working on from this, could you instead have your script
process via PHP (or similar)? That way it would be one script, but you
could use parameters in the script URL and have the PHP script change
the one line depending on the value, eg.

<script type=text/javascript"
src="http://example.com/script.php?type=iframe">

-or-

<script type=text/javascript"
src="http://example.com/script.php?type=frame">

That way you would be moving some of the logic to the PHP script rather
than the Javascript, but the end result would be the same.

--
Dylan Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.
 
S

Stevo

Dylan said:
Okay, so working on from this, could you instead have your script
process via PHP (or similar)? That way it would be one script, but you
could use parameters in the script URL and have the PHP script change
the one line depending on the value, eg.

<script type=text/javascript"
src="http://example.com/script.php?type=iframe">
-or-
<script type=text/javascript"
src="http://example.com/script.php?type=frame">

That way you would be moving some of the logic to the PHP script rather
than the Javascript, but the end result would be the same.

That could work, but it doesn't improve on what I've got. They currently
include the script by calling up frame.js or iframe.js. It doesn't solve
the issue I have, which is that they have to worry about which tag to
use. I want to tell them "just include script.js" and that script itself
figures out whether it's in a frame or iframe.

Thanks though :)
 
B

Bart Van der Donck

I think you might have skipped to the end a bit too quickly. I know
already if I'm in the top page (like you said I can test window==parent,
or top==self).

The situation I'm struggling with is when *I AM* in either a frame or an
iframe, and I want to know which of those two it is. Is it "iframe" or
is it "frame".

As a rule of thumb, you can never access this information when the
parent resides in another domain (Same Origin Policy).

But I think a few creative workarounds are still conceivable. E.g. if
your IFRAME is openend in 500*300pix, read out the current width and
height. The core idea is to read out a parameter inside the child
frame that was assigned by the parent.
 
S

Stevo

Bart said:
But I think a few creative workarounds are still conceivable. E.g. if
your IFRAME is openend in 500*300pix, read out the current width and
height. The core idea is to read out a parameter inside the child
frame that was assigned by the parent.

Thanks, that's a good suggestion. I might be able to make that work.
 

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,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top