Detecting when a frame is loaded without it's parent
Moderator: General Moderators
- 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
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!
Anyone got any ideas?
Ta!
Try something like this:
Main Page:
IFrame:
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>- mattcooper
- Forum Contributor
- Posts: 210
- Joined: Thu Mar 17, 2005 5:51 am
- Location: London, UK
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.
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.
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")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.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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)
;
}- mattcooper
- Forum Contributor
- Posts: 210
- Joined: Thu Mar 17, 2005 5:51 am
- Location: London, UK
-
bladecatcher
- Forum Commoner
- Posts: 67
- Joined: Sat Mar 12, 2005 12:50 am
Iframe - Direct call
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
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
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;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