Drinkin People Drinkin People - 4 months ago 34x
Javascript Question

Open iframe page in its parent when a direct link is to one of the child pages

Hi I have multiple pages that reside in an iframe and I would like them to open in the parent page when say a search engine indexes them or people directly link to them.

-this page will be the parent page and have an iframe
-this will be first page.php
-then there will be second page.php, third page.php, fourth page.php, ect.

I would like it if someone linked to any of the pages that require the iframe to load the parent page with the iframe and load the url into that iframe. So if someone links to say third page it will load the parent page and load third page in the iframe.

I have tried the following scripts that I have found from posts across the web but they only load the parent page with the original src in the frame.

<SCRIPT LANGUAGE='javascript'>
try {
if (top == self) {
top.location.href = 'parent page.php';
} catch(er) { }


<script type="text/javascript">
if (window == top) {
var url = 'window.location.replace("parent page.php?var1=' + window.location.href + '")';

I also tried:

<body onload="refreshFrame();">

Then I saw the post on Open iframe page in parent frame (javascript) here and it just brings up the page that was linked to and not the iframe.

The pages in the iframe are dymanically created as well so idk if that has anything to do with it or not.

But nothing seems to open the page inside of the iframe on my site it only loads the parent page with the original src first page in it. So idk if this is possible with either javascript or php.

Also idk if anyone knows how this practice effects seo there are not many words and no tags on the parent page so I was just curious about that too.



Did you try use the base tag in you iframe pages? Something like:

<base target="_parent" />


OK, so in your scenario you need to know if the current page is loaded on brownser or via iframe. And if it's not on iframe, redirect to your another page where you calling the iframe.

First thing first, check if the current page is inside a iframe with js:

<script type="text/javascript">
    if ( self === top ) {
        //Its not an Iframe;

Now that we know if the page isn't load via iframe we can use a redirect inside that if statament:


Now you need to make sure after redirect the page, the iframe src attribute will be the same url it was before. I'm assuming you can do that with a get parameter int the url? Let me know if find trouble in that part.


Just add a get parameter in the url as i mentioned above. Something like this:

<script type="text/javascript">
    if ( self === top ) {
        //Its not an Iframe, needs redirect:
        var url = window.location;

Now in your index.php ( or whatever is the page that contain the iframe):

    $iframe_url = (isset($_GET['iframe']) && !empty($_GET['iframe'])) ? $_GET['iframe'] : "initial-url-content";

<iframe src="<?=$iframe_url?>"></iframe>