Page 1 of 1
Detecting when a frame is loaded without it's parent
Posted: Tue Aug 09, 2005 12:11 pm
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!
Posted: Tue Aug 09, 2005 12:26 pm
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>
Posted: Tue Aug 09, 2005 1:05 pm
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...

Posted: Tue Aug 09, 2005 1:49 pm
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.
Posted: Tue Aug 09, 2005 2:16 pm
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)
;
}
Posted: Wed Aug 10, 2005 6:11 am
by mattcooper
Thanks guys, a big help as usual!
Posted: Mon Aug 22, 2005 3:18 pm
by pilau
Well that's exactly what I was searching for.
Posted: Mon Aug 22, 2005 10:56 pm
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.
Iframe - Direct call
Posted: Tue Sep 06, 2005 1:45 pm
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