Detecting when a frame is loaded without it's parent

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Detecting when a frame is loaded without it's parent

Post by mattcooper »

Page A is supposed to display in an iframe in page B... but a search engine has indexed page A, and links to it. I want page A to know whether it ias being displayed on its own, or whether is is present in a iframe. If it is on its own, I would like the code to redirect to page B with page A in the iframe where it should be.

Anyone got any ideas?

Ta!
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Try something like this:

Main Page:

Code: Select all

<body>
<iframe src="frame.htm"> </iframe>
<form action="" method="get" name="form1">
<input name="callme" id="callme" type="hidden" value="22">
</form>
</body>

IFrame:

Code: Select all

<script language="JavaScript">
function runOnload(){
if(parent.document.getElementById("callme"))
	alert('yes');
else
	alert('no');
}
</script>

<body onLoad="runOnload();">
I frame
</body>
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

Can you explain how this works? I'm afraid i don't see how this can be the best solution but forgive me if I'm being obtuse, it's been a long day... :)
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

The script isn't 100% complete just enough to get you going.

When your frame page loads ( frame.html )

OnLoad the following looks for an element named 'callme' located on the parent page.

Code: Select all

parent.document.getElementById("callme")
If the element is found you would do nothing ( Alert 'Yes' ) if it is not found you would do the redirect ( Alert 'No' )

All you have to do is replace the alerts() with you redirect.
Last edited by hawleyjr on Tue Aug 09, 2005 3:22 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

should check if parent is viable first ;)

Code: Select all

if(typeof(parent) == 'object' && typeof(parent.document) == 'object' && parent.document.getElementById && parent.document.getElementById('somethingUnique'))
{ // action to do when in a frame
  ;
}
else
{ // action to do when not in a frame (presumably, redirect to the framed version)
  ;
}
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

Thanks guys, a big help as usual!
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Well that's exactly what I was searching for.
bladecatcher
Forum Commoner
Posts: 67
Joined: Sat Mar 12, 2005 12:50 am

Post by bladecatcher »

I reckon it's pretty cool tooo :-)

now I'll have to work out the redirect ;-) (oh my brain hurts already)

Thanks guys
bladecatcher

btw, If your really paranoid might be an idea to generate a random code each time page is called.
User avatar
malcg
Forum Newbie
Posts: 3
Joined: Tue Sep 06, 2005 12:15 pm
Location: London, UK

Iframe - Direct call

Post by malcg »

I use Iframes within pages as portals, and so have to prevent direct loading

Try using this code in Iframe page before any headers sent

Code: Select all

if ( $_SERVER['REQUEST_URI'] == "/" ):
    header("Location: $owner");
    ob_end_flush();
    exit;
endif;
where $owner is host page name you want to send instead of Iframe page

this saves unnecessary downloads and is very fast

this also ensures Iframe is NOT indexed so you will need to add clever code to determine whether Spider or User request (as I do)

Good luck
Post Reply