Skip to content Skip to sidebar Skip to footer

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. Example:

Solution 1:

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

<base target="_parent" />

UPDATE

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;
    }
</script>

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

window.top.location="http://www.yourdomain.com";

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.

UPDATE2

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;
        window.location="http://www.yourdomain.com/index.php?iframe="+url;
    }
</script>

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

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

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

Post a Comment for "Open Iframe Page In Its Parent When A Direct Link Is To One Of The Child Pages"